Skip to content

Commit 02cd823

Browse files
committed
Maintain a backup cache for meetup events with a much longer expiry
1 parent c48b5cb commit 02cd823

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

pythonkc_site/meetups/events.py

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,38 @@
2727

2828

2929
def memoize_in_cache(cache_key, cache_timeout_seconds=None):
30-
"""Cache the result of a no-args function."""
30+
"""
31+
Caches the result of a no-args function. Further, a backup cache is
32+
maintained, updated as often as the primary but waits much longer to expire
33+
than the primary cache.
34+
35+
NOTE This should all go away once gondor gets schedule celery tasks that
36+
can be used to periodically populate the meetup.com event cache (thereby
37+
allowing visitor requests to avoid the hit).
38+
"""
39+
40+
backup_cache_key = cache_key + '_backup'
41+
backup_cache_timeout_seconds = (cache_timeout_seconds * 100
42+
if cache_timeout_seconds else
43+
60 * 60 * 24)
44+
3145
def decorator(func):
3246
@wraps(func)
3347
def decorated():
3448
value = cache.get(cache_key)
3549
if not value:
36-
value = func()
37-
cache.set(cache_key, value, cache_timeout_seconds)
50+
try:
51+
value = func()
52+
cache.set(cache_key, value, cache_timeout_seconds)
53+
cache.set(backup_cache_key, value,
54+
backup_cache_timeout_seconds)
55+
except:
56+
value = cache.get(backup_cache_key)
57+
if value:
58+
logging.exception('Had to load backup value due to '
59+
'exception')
60+
else:
61+
raise
3862
return value
3963
return decorated
4064
return decorator

0 commit comments

Comments
 (0)