Skip to content

Commit

Permalink
actually set the redis expiration
Browse files Browse the repository at this point in the history
  • Loading branch information
bbangert committed Feb 1, 2012
1 parent 7ad22e4 commit c3da7e2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
7 changes: 6 additions & 1 deletion retools/cache.py
Expand Up @@ -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,
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions retools/tests/test_cache.py
Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit c3da7e2

Please sign in to comment.