Implements high level function caching to Redis with a decorator
pip install redis_cache_decorator
from 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():
...
invalidator
: Boolean to determine whether or not to return a cache invalidating function
e.g.
@r.cache(invalidator=True)
def my_method():
...
Now when you call my_method
, it will return two values. The first value is the cached return if a cache hit occurs, otherwise it's the return value from executing the function.
The second value is a callable function to invalidate the cache.
return_value, invalidator = my_method()
To invalidate the cached return, just call the invalidator:
invalidator()
Check for any open issues, or open one yourself! All contributions are appreciated.
nosetests