[docs]classMemoryCache(BaseCache):'''A Memory cache to store temporary items Args: default_ttl (int, *optional*): the default time_to_live in seconds for each item, defaults to 10'''def__init__(self,default_ttl:int=10):self.items:Dict[str,CachedItem]={}self.default_ttl=default_ttl
[docs]defset(self,key:str,value:Any,ttl:int=None)->Optional[CachedItem]:'''create a CachedItem with the ttl specified or the default_tll Args: key (str): the item key value (Any): the cached value ttl (int, *optional*): item time_to_live, defaults to self.default_ttl Returns: Optional[:class:`tgram_dnd.caching.cached_item.CachedItem`]'''ifkeyinself.items:returnself.items[key]=CachedItem(value=value,ttl=ttlorself.default_ttl)returnself.items[key]
[docs]defget(self,key:str)->Optional[Any]:'''get the CachedItem by key if has not expired Args: key (str): the item key Returns: Optional[Any]: the cached value'''ifkeynotinself.items:returnitem=self.items[key]ifitem.has_expired:self.items.pop(key)returnreturnitem.get()
[docs]asyncdefget_or_create(self,key:str,value:Callable,ttl:int=None)->CachedItem:'''get the CachedItem by key, and if not exists a new CachedItem will be created and returned Args: ket (str): the item key value (Callable): the function that will retrieve the item value (in case the item does not exist/expired) ttl (int, *optional*): item time_to_live, defaults to self.default_ttl Returns: Any: The cached Value'''item=self.get(key)ifitem:returnitemitem=self.set(key,(awaitrun_function(value)),ttl,)returnitem.get()