Package: cachecall
CachecallDescriptionA cache library for sync and async functions with ttl and expiration time.FeaturesAlgorithm (FIFO)TTLExpire TimeCache groupsExamplesCache regular (sync) functionsCache async functionsCache with max sizeCache with ttl (time to live)Cache with expire timeCache groupClean cache groupKeep cache ignoring some keysCache with memory max sizeAccess cache objectDocumentationCache decoratorCache objectExpire Time classAlgorithmThe FIFO Algorithm is used to control the input and output of data for cache groups.Cache groupsBy default each function has its own cache group to keep the cached data separated. Is possible create a shared cache group between different functions or clear cache group usingclear_cache("group_name").TTL (Time To Live)By default thecachedecorator has the ttl parameter to definetime to livein seconds to expire the cached data.Expire TimeWithcachecall.ExpireTimeis possible define a specific time to cached data expire every day.Example here.ExamplesCache regular (sync) functionsfromcachecallimportcache@cache()deffunc(x=None):print("call")return10Cache async functionsfromcachecallimportcache@cache()asyncdefafunc(x=None):print("call")return15awaitafunc()Cache with Max Sizefromcachecallimportcache@cache(max_size_call=2)deffunc(x):print("call")returnxfunc("a")# Add value "a" in cachefunc("b")# Add value "b" in cachefunc("c")# Remove value "a" from cache and Add value "c"func("d")# Remove value "b" from cache and Add value "d"Cache with TTL (Time To Live)fromcachecallimportcache@cache(ttl=10)deffunc(x=1):print("call")return"abc"Each 10 seconds the cache for the functionfuncwill be expired.Cache with Expire Timefromcachecallimportcache,ExpireTime@cache(expire_time=ExpireTime(10,15,20))deffunc(a=None):print("call")return"xyz"Every day at 10:15:20 the value cached will expire.If theExpireTime(10, 15, 20)will defined before 10:15:20 in current day, the the data cached will expired in same day, but if ExpireTime will defined after the 10:15:20, the cached value will be expired in next day.You can expire cache in several specific hours using a tuple of several ExpireTime.@cache(expire_time=(ExpireTime(6,0,0),ExpireTime(12,0,0),ExpireTime(18,0,0),))deffunc(a):...TTL with Expire Timefromcachecallimportcache,ExpireTime@cache(ttl=18000,expire_time=ExpireTime(6,0,0))deffunc(a=None):print("call")return"xyz"ttlandexpire_timecan be used together. In this example the cached data will expire each 5 hours and every day at 6:00:00.Cache groupfromcachecallimportcache@cache(group_name='some_group_name')defdo_a(x=None):print("call a")return"xyz"@cache(group_name='some_group_name')defdo_b(x=None):print("call abc")return"abc"All the cached data will be stored inside a cache group called "some_group_name".Even though you use many different functions with the same parameters inside the same cache group, all cached data are associated with your specific function.Clean cache groupfromcachecallimportcache,clear_cache@cache(group_name='some_group_name')deffunc(x=None):print("call")return"xyz"clear_cache('some_group_name')# Clear all cached data inside some_group_nameKeep cache ignoring some keysfromcachecallimportcache@cache(ignore_keys=("y"))deffunc(x=1,y=None):print("call")returnxfunc(1,y=1)# Cache datafunc(1,y=2)# the y value is ignored and the cached data is returnedfunc(1,y=12345)# the y value is ignored and the cached data is returnedfunc(2,y=12345)# Add new data in cacheWhen some parameter is defined in ignore keys tuple these parameter value is ignored in cache creation, and even though the value of this parameter is changed the data will be kept cached.Currently this feature only works if the ignored parameters are used as named arguments.Cache with memory max sizefromcachecallimportcache,kbfake_lg1=''.join([str(val)forvalinrange(500)])# sizeof: 1439 bytesfake_lg2='-'.join([str(val)forvalinrange(400)])# sizeof: 1538 bytes@cache(max_size_mem=kb(2))# defining a limit of usage memory for 2 kilobytes (2048 bytes)deffunc(param):print("call")ifparam=="a":returnfake_lg1returnfake_lg2func("a")# Add fake_lg1 in cachefunc("a")# Using cachefunc("b")# Remove fake_lg1 from cache because of memory limit 2048 bytes and add fake_lg2Access cache objectfromcachecallimportcache@cache()deffunc(x):print('call')returnxfunc(1)# add in cachefunc(1)# Use cached datafunc(2)# add in cache# Access cached datafunc.cache.data# Cached dataSee more aboutcache object.DocumentationCache DecoratorfromcachecallimportcacheParametersmax_size_call:Optional[int]Number of values (or call to cached function) that will be stored in cache.If None the cache size will be unlimited.Default value is None.group_name:Optional[str]Group name for cached data.Each function has their own cache group, but if you wish to use the same cached data for different functions just use the samegroup_namefor these functions.If None the group name default will be "function-name_uuid4".Default value is None.ttl:Optional[Union[int, float]](Time To Live) Value in seconds to expire the cached data.If None the value never expires.Default value is None.expire_time:Optional[Union[ExpireTime, Tuple[ExpireTime]]]Specific time to expire a cached value daily.You can use a tuple (or list) ofExpireTimeobjects for defining several specific times to expire data.If None the value never expires.Default value is None.ignore_keys:Optional[Tuple[str]]Tuple of parameters name that will be ignored in cache.If some cached data exists and the value of some ignored_key is changed, the data will be continued cached because all arguments mapped in ignore_keys tuple will be ignored.Currently this feature only works if the ignored parameters are used as named arguments.Observation:Even though the cached function (function decorated bycachecall.cachedecorator) no has any defined parameter the data of this function will be cached using a "no-key" key for identifying cached data.Theignore_keyshave be used if your cached function (function decorated bycachecall.cachedecorator) has at least two parameters or more, because, if your function has only one parameter and this parameter is defined in ignore_keys, that means that your function will cache data normally because the only defined parameter will be ignored. Therefore, usingignore_keysfor cached functions with only one defined parameter and this only parameter name passed as value inignore_keyswill have no effect and the data will be cached normally.This behavior also will occur if you have many parameters and all parameters names are passed toignore_keys.log_level:Optional[Union[int, str]]This parameter is useful for you to see some behaviors that's occurring in data caching.Default value islogging.WARNING.max_size_mem:Optional[Union[int, float]]Define a limit of memory usage in bytes for each cache group. All cached data always have a memory size less or equal to the value defined in max_size_mem.If a unique data has a size memory greater than the value defined inmax_size_memthis value will not be cached. This operation will be logged as debug level.Default value is None. Thats mean that no exists memory limit for cache group.Functions for control cache unity:from cachecall import kb, mb, gbkb(val): return thevalvalue converted to bytes. For example:kb(1) = 1024.mb(val): return thevalvalue converted to bytes. For example:mb(1) = 1048576.gb(val): return thevalvalue converted to bytes. For example:gb(1) = 1073741824.Cache objectThis object is used to control the cache data.Each cached function will have a cache object.The goal in adding this method in the cached function is for debug purposes usingfunc.cache.data.We recommend strongly that you won't handle any operation using this method so you do not cause any unexpected error.Expire Time classfromcachecallimportExpireTime@dataclassclassExpireTime:hour:intminute:intsecond:int