Package: persistent-cache-decorator
persistent-cache-decoratorTable of Contentspersistent-cache-decoratorInstallationUsageCached PropertyCreating a custom cache backendLicenseInstallationpip install persistent-cache-decoratorUsagefrom__future__importannotationsimporttimefrompersistent_cache.backendimportCacheBackendfrompersistent_cache.decoratorsimportjson_cachefrompersistent_cache.decoratorsimportpersistent_cache@persistent_cache(minutes=4)deflong_func(n:int)->str:"""Long Func Documentation."""# Long functiontime.sleep(n)returnf"{n}"# reveal_type(long_func)# Runtime type is "_persistent_cache"# <persistent_cache_decorator._persistent_cache object at 0x10468be50># Call function(takes 5 seconds)long_func(5)"5"# Call function again (takes 0 seconds)long_func(5)"5"# Bypass caching(takes 5 seconds)long_func.no_cache_call(5)"5"# Call function again (takes 0 seconds)long_func(5)"5"# Clear cache for this functionlong_func.cache_clear()# Call function(takes 5 seconds)long_func(5)Cached PropertyfromtypingimportNamedTuple# noqa: E402frompersistent_cache.decoratorsimportjson_cached_property# noqa: E402# To cache instance methods, use the json_cache decorator you can do the following:# Reference: https://www.youtube.com/watch?v=sVjtp6tGo0gclassPet:def__init__(self,name:str,age:int)->None:self.name=nameself.age=age# creating the cache function this way will allow the cache to be cleared using the instance# It will also only use the arguments as the keyself.online_information=json_cache(days=2)(self._online_information)def_online_information(self,source:str)->int:# Something that takes a long timereturnlen(source)pet=Pet("Rex",5)pet.online_information(source="https://api.github.com/users/rex")pet.online_information.cache_clear()# NEW: or you can use the json_cached_property decorator to cache the result of a method# This makes use of Python's Descriptors: https://www.youtube.com/watch?v=vBys0SwYvCQclassPerson(NamedTuple):name:strage:int# The decorator works with Namedtuples as well as with classes@json_cached_property(days=2)defonline_information(self,source:str)->int:# Something that takes a long timereturnlen(source)person=Person("John",30)# The following call will cache the result using the class instance as well as the arguments as the key  # noqa: E501person.online_information(source="https://api.github.com/users/john")# To clear the cache, use the method from the class directlyPerson.online_information.cache_clear()Creating a custom cache backendfromtyping_extensionsimportAny# noqa: E402fromtypingimportCallable# noqa: E402frompersistent_cache.decoratorsimportcache_decorator_factory# noqa: E402fromtypingimportTYPE_CHECKING# noqa: E402ifTYPE_CHECKING:importdatetimefrompersistent_cache.decoratorsimport_R# Define a custom cache backendclassRedisCacheBackend(CacheBackend):defget_cache_or_call(# type: ignore[empty-body]self,*,func:Callable[...,_R],args:tuple[Any,...],kwargs:dict[str,Any],lifespan:datetime.timedelta,)->_R:...defdel_func_cache(self,*,func:Callable[...,Any])->None:...# Singleton InstanceREDIS_CACHE_BACKEND=RedisCacheBackend()# Quick way of defining a decorator. You can use this if you want multiple decorators with different cache durations.  # noqa: E501redis_cache=cache_decorator_factory(backend=REDIS_CACHE_BACKEND)@redis_cache(days=1)deffoo(time:float)->float:fromtimeimportsleepsleep(time)returntimeLicensepersistent-cache-decoratoris distributed under the terms of theMITlicense.