Compute the memcache_key only in get()
authorSverre Rabbelier <srabbelier@gmail.com>
Sat, 21 Feb 2009 00:45:19 +0000
changeset 1418 50e989482d1b
parent 1417 8b9e256a3abe
child 1419 5d0f80ad7b9f
Compute the memcache_key only in get() This will be used by the homepage caching later on. Patch by: Sverre Rabbelier
app/soc/cache/base.py
app/soc/cache/sidebar.py
--- a/app/soc/cache/base.py	Sat Feb 21 00:43:56 2009 +0000
+++ b/app/soc/cache/base.py	Sat Feb 21 00:45:19 2009 +0000
@@ -38,12 +38,15 @@
   
     @wraps(func)
     def wrapper(*args, **kwargs):
-      result = get(*args, **kwargs)
+      result, key = get(*args, **kwargs)
       if result:
         return result
 
       result = func(*args, **kwargs)
-      put(result, *args, **kwargs)
+
+      if key:
+        put(result, key, *args, **kwargs)
+
       return result
 
     return wrapper
--- a/app/soc/cache/sidebar.py	Sat Feb 21 00:43:56 2009 +0000
+++ b/app/soc/cache/sidebar.py	Sat Feb 21 00:45:19 2009 +0000
@@ -41,20 +41,19 @@
   """
 
   memcache_key = key(id)
-  return memcache.get(memcache_key)
+  return memcache.get(memcache_key), memcache_key
 
 
-def put(sidebar, id, user):
+def put(sidebar, memcache_key, id, user):
   """Sets the sidebar for the specified user in the memcache.
 
   Args:
     sidebar: the sidebar to be cached
   """
 
-  # Store sidebar for ten minutes since new programs might get added
+  # Store sidebar for just three minutes to force a refresh every so often
   retention = 3*60
 
-  memcache_key = key(id)
   memcache.add(memcache_key, sidebar, retention)