From c3da7e2b45e9ed466dd62c7ee6a1a5ddec47923f Mon Sep 17 00:00:00 2001 From: Ben Bangert Date: Wed, 1 Feb 2012 11:36:25 -0800 Subject: [PATCH] actually set the redis expiration --- retools/cache.py | 7 ++++++- retools/tests/test_cache.py | 8 ++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/retools/cache.py b/retools/cache.py index 431d4a3..b469dc7 100644 --- a/retools/cache.py +++ b/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) diff --git a/retools/tests/test_cache.py b/retools/tests/test_cache.py index baf9826..fae8ba6 100644 --- a/retools/tests/test_cache.py +++ b/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):