Implements high level function caching to Redis with a decorator
pip install redis_cache_decorator
from redis_cache.redis_cache import RedisCache
r = RedisCache('localhost', 6379)
@r.cache()
def my_method(a, b, c):
return a ** b ** c
expiration
: Number of seconds to keep the result in the cache. Defaults to 60 seconds when not specified.
e.g.
@r.cache(expiration=100)
def my_method():
...
signature_generator
: Callable function that generates the signature to cache on. The default signature generator will be used if not specified.
e.g.
def sig_gen(*args, **kwargs):
return "?".join(args)
r.cache(signature_generator=sig_gen)
def my_method():
...
Check for any open issues, or open one yourself! All contributions are appreciated.
nosetests
When implementing the caching decorator on methods with objects as parameters, please be sure to override the __str__
method on that object. The default behavior for most objects in Python is to return a string such as <Object instance at 0x12345678>
. Since this is in the signature of the function, it will cache using that address in memory and will result in cache misses everytime that object is changed. This is especially apparent while caching class methods since the first paramter is always the object itself (self
).