Synchronous data cache with redis
Python Cache client for redis.
Supports Python 3.5 and above.
Through pip: pip install pyredis-cache
Manually: python setup.py install
. Install dependencies first
# Import redis Connection pool and CacheClient.
from redis import ConnectionPool
from redis_cache import CacheClient
import json
# Initialize the Connection pool.
redis_pool = ConnectionPool()
# Create a data synchronisation function
# The first argument should define the uniqueness of the data
def sync_func(student_id, lots_of_args):
# fetch the data however you want to
return data # Lets assume that the data returned is of type `dict`
# Initialize the cache client
cache = 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: 12
cache.set(12)
# This will set cache for ID: 13, with custom data
cache.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: 12
cache.delete(12)
# Import redis Connection pool and HashCacheClient.
from redis import ConnectionPool
from redis_cache import CacheClient
import json
# Initialize the Connection pool.
redis_pool = ConnectionPool()
# Create a data synchronisation function
# The first argument should define the uniqueness of the data
def sync_func(student_id, lots_of_args):
# fetch the data however you want to
return data # 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: 3
hcache.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: 3
hcache.delete(3, 12)
# This will delete the hash with hash_id: 3
hcache.delete_hash(3)
student
(type string)12
(type int)json.dumps
json.loads
grade12
and hash_id can be 2
Key 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.
If your function is Async i.e. makes use of async
and await
coroutines (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)
async def some_func():
data = await cache.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)