Package: pyredis-cache
pyredis-cachePython Cache client for redis.Supports Python 3.5 and above.FeaturesAutomatically Sync data between the sync_func and cacheUse of Async/Await for Asynchronous execution of sync_funcDependenciesredis-pyhiredis-pyInstallationThrough pip:pip installpyredis-cacheManually:python setup.py install. Install dependencies firstGetting StartedSimple Redis Cache# Import redis Connection pool and CacheClient.fromredisimportConnectionPoolfromredis_cacheimportCacheClientimportjson# Initialize the Connection pool.redis_pool=ConnectionPool()# Create a data synchronisation function# The first argument should define the uniqueness of the datadefsync_func(student_id,lots_of_args):# fetch the data however you want toreturndata# Lets assume that the data returned is of type `dict`# Initialize the cache clientcache=CacheClient(redis_pool,"Student",sync_func,set_func=json.dumps,get_func=json.loads,expire_time=10)# for persistent cache, assign 0 to expire_time (default)# This will set cache for ID: 12cache.set(12)# This will set cache for ID: 13, with custom datacache.set(13,data={"name":"John Doe","age":16})# This will get cache for ID: 12 (no need to do a set, it will automatically set the data)cache.get(12)# The get function will pass extra arguments to the sync_func, same works with set.cache.get(14,age=15)# This will delete the cache for ID: 12cache.delete(12)Caching into a single hash. I am still working on this to make a much
more flexible caching mechanism# Import redis Connection pool and HashCacheClient.fromredisimportConnectionPoolfromredis_cacheimportCacheClientimportjson# Initialize the Connection pool.redis_pool=ConnectionPool()# Create a data synchronisation function# The first argument should define the uniqueness of the datadefsync_func(student_id,lots_of_args):# fetch the data however you want toreturndata# Lets assume that the data returned is of type `dict`# Initialize the cache client.hcache=HashCacheClient(redis_pool,"Class","Student",sync_func,set_func=dumps,get_func=loads)# expire_time not supported.# This will set cache for ID: 12 for hash_id: 3hcache.set(3,12)# This will get cache for ID: 12 for hash_id: 3 (no need to do a set, it will automatically set the data)hcache.get(3,12)# This will delete the cache for ID: 12 for hash_id: 3hcache.delete(3,12)# This will delete the hash with hash_id: 3hcache.delete_hash(3)Terminologykey: is the identifier for the cache object. eg.student(type
string)identity: is the index for a particular key. eg. Roll no. of a
student12(type int)sync_func: function which will give data tto be stores in cache.set_func: data parsing function for setting data into cache. eg. if
you want to set a dict value, you will have to set this asjson.dumpsget_func: data parsing function for getting data into cache. eg. if
you want to set a dict value, you will have to set this asjson.loadshash_key and hash_id: Unique identifiers for the hash. eg.
hash_key can begrade12and hash_id can be2Key and Identity are combined in a unique fashion to make the name
against which the cache object will be stored. Similarly for hash_key
and hash_id.AsyncIf your function is Async i.e. makes use ofasyncandawaitcoroutines (PEP 492).
You will have to use event loop to run use this functionality like
asyncio and tornado. You could do the following:-cache=CacheClient(redis_pool,"Student",sync_func,set_func=json.dumps,get_func=json.loads,expire_time=10,asynchronous=True)asyncdefsome_func():data=awaitcache.get(23)Note: the cache client is synchronous because redis servers are quite
fast. You can expect near 0 latency with the correct setup. Also the
creator of tornado-redis suggests this kind if architecture to reduce
the cost overhead caused by setting up callbacks. (see post:http://stackoverflow.com/a/15596969/2248966)