Package: scrooge-cache
Scrooge CacheWhat is Scrooge?Scrooge is aSmartCache Storage for strongergentlemenBackend supports:memcacheredisHow can I use?Scrooge is able to cache function returns based on its input arguments for a given time.Rules:Just a unique namespace per backend instance;If you do not set expiration_time scrooge will take infinite time;The return of decorated function must be str or int or float or tuple, or list or dict;If you use redis backend you can defined the db index using the parameterdb=index, if you do not do this the default will be 0;Installingpip install scrooge-cacheQuick startUsing with redis as backendThis example below will cache the function return for an undetermined timeimporttimefromscroogeimportRedisBackend,Clientbackend=RedisBackend(host='127.0.0.1',port=6379)client=Client(cache_backend=backend)# Cached for an undetermined time@client.gentlemen_cache(namespace='f1')deffunction_to_be_cached(p1,p2):time.sleep(5)return{"p1":p1,"p2":p2}# After 5 seconds the return will be {"p1": 4, "p2": 5}print(function_to_be_cached(4,5))# The return will be {"p1": 4, "p2": 5}print(function_to_be_cached(4,5))This example below will cache the function return for 10 secondsimporttimefromscroogeimportRedisBackend,Clientbackend=RedisBackend(host='127.0.0.1',port=6379)client=Client(cache_backend=backend)# Cached for 10 seconds@client.gentlemen_cache(namespace='f1',expiration_time=10)deffunction_to_be_cached(p1,p2):time.sleep(5)return{"p1":p1,"p2":p2}# After 5 seconds the return will be {"p1": 4, "p2": 5}print(function_to_be_cached(4,5))# The return will be {"p1": 4, "p2": 5}print(function_to_be_cached(4,5))time.sleep(5)# After 5 seconds the return will be {"p1": 4, "p2": 5}print(function_to_be_cached(4,5))Using with memcache as backendThis example below will cache the function return for an undetermined timeimporttimefromscroogeimportMemcacheBackend,Clientbackend=MemcacheBackend(host='127.0.0.1',port=11211)client=Client(cache_backend=backend)# Cached for an undetermined time@client.gentlemen_cache(namespace='f1')deffunction_to_be_cached(p1,p2):time.sleep(5)return{"p1":p1,"p2":p2}# After 5 seconds the return will be {"p1": 4, "p2": 5}print(function_to_be_cached(4,5))# The return will be {"p1": 4, "p2": 5}print(function_to_be_cached(4,5))This example below will cache the function return for 10 secondsimporttimefromscroogeimportMemcacheBackend,Clientbackend=MemcacheBackend(host='127.0.0.1',port=11211)client=Client(cache_backend=backend)# Cached for 10 seconds@client.gentlemen_cache(namespace='f1',expiration_time=10)deffunction_to_be_cached(p1,p2):time.sleep(5)return{"p1":p1,"p2":p2}# After 5 seconds the return will be {"p1": 4, "p2": 5}print(function_to_be_cached(4,5))# The return will be {"p1": 4, "p2": 5}print(function_to_be_cached(4,5))time.sleep(5)# After 5 seconds the return will be {"p1": 4, "p2": 5}print(function_to_be_cached(4,5))No cachingIf you wanna a cacheless request, you can do this using theno_cacheargumentimporttimefromscroogeimportMemcacheBackend,Clientbackend=MemcacheBackend(host='127.0.0.1',port=11211)client=Client(cache_backend=backend)# Cached for 10 seconds@client.gentlemen_cache(namespace='f1',expiration_time=10)deffunction_to_be_cached(p1,p2):time.sleep(5)return{"p1":p1,"p2":p2}# After 5 seconds the return will be {"p1": 4, "p2": 5}print(function_to_be_cached(4,5))# Cacheless request below# After 5 seconds the return will be {"p1": 4, "p2": 5}print(function_to_be_cached(4,5,no_cache=True))Force cache updateIf you wanna force the cache update, you can do this using theforce_cache_updateargumentimporttimefromscroogeimportMemcacheBackend,Clientbackend=MemcacheBackend(host='127.0.0.1',port=11211)client=Client(cache_backend=backend)# Cached for 10 seconds@client.gentlemen_cache(namespace='f1',expiration_time=10)deffunction_to_be_cached(p1,p2):time.sleep(5)return{"p1":p1,"p2":p2}# After 5 seconds the return will be {"p1": 4, "p2": 5}print(function_to_be_cached(4,5))# Force cache update# After 5 seconds the return will be {"p1": 4, "p2": 5}print(function_to_be_cached(4,5,force_cache_update=True))Run testspytest -ra