Browse files

actually set the redis expiration

  • Loading branch information...
1 parent 7ad22e4 commit c3da7e2b45e9ed466dd62c7ee6a1a5ddec47923f @bbangert committed Feb 1, 2012
Showing with 10 additions and 5 deletions.
  1. +6 −1 retools/cache.py
  2. +4 −4 retools/tests/test_cache.py
View
7 retools/cache.py
@@ -92,13 +92,16 @@ class CacheRegion(object):
statistics = True
@classmethod
- def add_region(cls, name, expires, redis_expiration=60 * 60):
+ def add_region(cls, name, expires, redis_expiration=60 * 60 * 24 * 7):
"""Add a cache region to the current configuration
:param name: The name of the cache region
:type name: string
:param expires: The expiration in seconds.
:type expires: integer
+ :param redis_expiration: How long the Redis key expiration is
+ set for. Defaults to 1 week.
+ :type redis_expiration: integer
"""
cls.regions[name] = dict(expires=expires,
@@ -186,6 +189,7 @@ def load(cls, region, namespace, key, regenerate=True, callable=None,
now = time.time()
region_settings = cls.regions[region]
expires = region_settings['expires']
+ redis_expiration = region_settings['redis_expiration']
keys = CacheKey(region=region, namespace=namespace, key=key)
@@ -240,6 +244,7 @@ def load(cls, region, namespace, key, regenerate=True, callable=None,
p = redis.pipeline(transaction=True)
p.hmset(keys.redis_key, {'created': now,
'value': cPickle.dumps(value)})
+ p.expire(keys.redis_key, redis_expiration)
cls._add_tracking(p, region, namespace, key)
if statistics:
p.getset(keys.redis_hit_key, 0)
View
8 retools/tests/test_cache.py
@@ -59,7 +59,7 @@ def a_func():
assert 'This is a value' in value
exec_calls = [x for x in mock_pipeline.method_calls \
if x[0] == 'execute']
- eq_(len(mock_pipeline.method_calls), 10)
+ eq_(len(mock_pipeline.method_calls), 11)
eq_(len(exec_calls), 2)
def test_existing_value_no_regen(self):
@@ -108,7 +108,7 @@ def a_func():
assert 'This is a value' in value
exec_calls = [x for x in mock_pipeline.method_calls \
if x[0] == 'execute']
- eq_(len(mock_pipeline.method_calls), 10)
+ eq_(len(mock_pipeline.method_calls), 11)
eq_(len(exec_calls), 2)
def test_value_expired_and_no_lock(self):
@@ -136,7 +136,7 @@ def a_func():
assert 'This is a value' in value
exec_calls = [x for x in mock_pipeline.method_calls \
if x[0] == 'execute']
- eq_(len(mock_pipeline.method_calls), 10)
+ eq_(len(mock_pipeline.method_calls), 11)
eq_(len(exec_calls), 2)
def test_generate_value_no_stats(self):
@@ -164,7 +164,7 @@ def a_func():
assert str(now) in value
exec_calls = [x for x in mock_pipeline.method_calls \
if x[0] == 'execute']
- eq_(len(mock_pipeline.method_calls), 5)
+ eq_(len(mock_pipeline.method_calls), 6)
eq_(len(exec_calls), 1)
def test_generate_value_other_creator(self):

0 comments on commit c3da7e2

Please sign in to comment.