So I am working on a Customer class that is supposed to be wrapper for some other classes that retrieve information about a specific customer from a server and from online, as below.
class Customer:
def __init__(self, name):
self.name = name
@property
@lru_cache()
def online_info(self):
print('retrieving customer online info')
return Online().search_result(for=self)
@property
@lru_cache()
def server_info(self):
print('retrieving customer server info')
return Server().search_result(for=self)
The online and server calls have to be @property decorated. The problem I am facing is when trying to cache the online_info and server_info calls. The cache would somehow have to be at a class level so that even if a news customer is instantiated, the lru_cache wold remember previous calls from other instantiations for the same name call. Note my print statements. This is the behavious I am trying to achieve:
>>> cutomer1 = Customer('John')
>>> customer1.online_info
retrieving customer online info
John Smith has the following info bla bla bla ....
>>> cutomer2 = Customer('John')
>>> customer2.online_info # this one will not be calling the function, lru_cache will return the value saved from customer1.online_info
John Smith has the following info bla bla bla ....
Can someone explain how I achieve this behaviour? Is this possible?
Customer('not John').online_info? Should that also use the cached value?lru_cacheon properties, or at least, there is no point in usinglru_cachethere even if you got your decorator order right (which is incorrect as it is now).lru_cacheworks by looking at the arguments to a function, and here all you have is... nothing, as properties don't take arguments.selfargument. The code works perfectly fine if the decorators are swapped.