Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve @redis_cache() #56

Merged
merged 3 commits into from
Sep 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pottery/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@


__title__ = 'pottery'
__version__ = '0.44'
__version__ = '0.45'
__description__, __long_description__ = (
s.strip() for s in __doc__.split('\n\n', 1)
)
Expand Down
22 changes: 18 additions & 4 deletions pottery/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,36 @@



_DEFAULT_TIMEOUT = 365 * 24 * 60 * 60



def _arg_hash(*args, **kwargs):
return hash((args, frozenset(kwargs.items())))

def redis_cache(*, key, redis=None):


def redis_cache(*, key, redis=None, timeout=_DEFAULT_TIMEOUT):
'''Redis-backed caching decorator.

Arguments to the cached function must be hashable.

Access the underlying function with f.__wrapped__.
'''
redis = Redis() if redis is None else redis
cache = RedisDict(redis=redis, key=key)

def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
key = _arg_hash(*args, **kwargs)
hash_ = _arg_hash(*args, **kwargs)
try:
return_value = cache[key]
return_value = cache[hash_]
except KeyError:
return_value = func(*args, **kwargs)
cache[key] = return_value
cache[hash_] = return_value
redis.expire(key, timeout)
return return_value
wrapper.__wrapped__ = func
return wrapper
return decorator
20 changes: 20 additions & 0 deletions tests/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@


import random
import time

from pottery import RedisDict
from pottery import redis_cache
from pottery.base import _default_redis
from pottery.cache import _DEFAULT_TIMEOUT
from tests.base import TestCase


Expand Down Expand Up @@ -67,3 +69,21 @@ def test_cache(self):
assert value5 != value4
assert self.expensive_method('raj', last='shah') == value5
assert len(self.cache) == 4

def test_expiration(self):
self.expensive_method()
assert self.redis.ttl(self._KEY) == _DEFAULT_TIMEOUT
time.sleep(1)
assert self.redis.ttl(self._KEY) == _DEFAULT_TIMEOUT - 1

self.expensive_method()
assert self.redis.ttl(self._KEY) == _DEFAULT_TIMEOUT
time.sleep(1)
assert self.redis.ttl(self._KEY) == _DEFAULT_TIMEOUT - 1

self.expensive_method('raj')
assert self.redis.ttl(self._KEY) == _DEFAULT_TIMEOUT

def test_wrapped(self):
assert self.expensive_method() == self.expensive_method()
assert self.expensive_method() != self.expensive_method.__wrapped__()