Skip to content

Commit

Permalink
Only return stale value after trying to acquire Lock.
Browse files Browse the repository at this point in the history
  • Loading branch information
dholth committed Jan 13, 2012
1 parent ca0acf2 commit 916d2c6
Showing 1 changed file with 29 additions and 22 deletions.
51 changes: 29 additions & 22 deletions retools/cache.py
Expand Up @@ -219,31 +219,38 @@ def load(cls, region, namespace, key, regenerate=True, callable=None,
# there's no callable, so we return a NoneMarker
return NoneMarker

# We either have a result and its expired, or no result
# Does someone already have the lock? If so, return the value if
# we have one
if result and redis.exists(keys.lock_key):
return cPickle.loads(result['value'])
# Don't wait for the lock if we have an old value
if result and 'value' in result:
timeout = 0
else:
timeout = 60 * 60

with Lock(keys.lock_key, expires=expires, timeout=60 * 60 * 24 * 7):
# Did someone else already create it?
result = redis.hgetall(keys.redis_key)
now = time.time()
if result and 'value' in result and \
now - float(result['created']) < expires:
try:
with Lock(keys.lock_key, expires=expires, timeout=60 * 60 * 24 * 7):
# Did someone else already create it?
result = redis.hgetall(keys.redis_key)
now = time.time()
if result and 'value' in result and \
now - float(result['created']) < expires:
return cPickle.loads(result['value'])

value = callable()

p = redis.pipeline(transaction=True)
p.hmset(keys.redis_key, {'created': now,
'value': cPickle.dumps(value)})
cls._add_tracking(p, region, namespace, key)
if statistics:
p.getset(keys.redis_hit_key, 0)
new_hits = int(p.execute()[0])
else:
p.execute()
except LockTimeout:
if result:
return cPickle.loads(result['value'])

value = callable()

p = redis.pipeline(transaction=True)
p.hmset(keys.redis_key, {'created': now,
'value': cPickle.dumps(value)})
cls._add_tracking(p, region, namespace, key)
if statistics:
p.getset(keys.redis_hit_key, 0)
new_hits = int(p.execute()[0])
else:
p.execute()
# log some sort of error?
return NoneMarker

# Nothing else to do if not recording stats
if not statistics:
Expand Down

0 comments on commit 916d2c6

Please sign in to comment.