Cache sidebar with a simple caching API
authorSverre Rabbelier <srabbelier@gmail.com>
Sun, 25 Jan 2009 16:28:07 +0000
changeset 987 6fd5c561b446
parent 986 e9611a2288ca
child 988 e35b3d98d469
Cache sidebar with a simple caching API We cache the sidebar once for every user, the downside is that once the user meets new access criteria, their sidebar does not get updated. This can be fixed by calling soc.cache.sidebar.flush() when appropriate. Patch by: Sverre Rabbelier
app/soc/cache/__init__.py
app/soc/cache/base.py
app/soc/cache/sidebar.py
app/soc/views/helper/decorators.py
app/soc/views/helper/responses.py
app/soc/views/sitemap/sidebar.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/app/soc/cache/base.py	Sun Jan 25 16:28:07 2009 +0000
@@ -0,0 +1,51 @@
+#!/usr/bin/python2.5
+#
+# Copyright 2008 the Melange authors.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+"""Module containing some basic caching functions.
+"""
+
+__authors__ = [
+    '"Sverre Rabbelier" <sverre@rabbelier.nl>',
+  ]
+
+
+from functools import wraps
+
+
+def getCacher(get, put):
+  """Returns a caching decorator that uses get and put
+  """
+
+  # TODO(SRabbelier) possibly accept 'key' instead, and define
+  # get and put in terms of key, depends on further usage
+
+  def cache(func):
+    """Decorator that caches the result from func
+    """
+  
+    @wraps(func)
+    def wrapper(*args, **kwargs):
+      result = get(*args, **kwargs)
+      if result:
+        return result
+
+      result = func(*args, **kwargs)
+      put(result)
+      return result
+
+    return wrapper
+
+  return cache
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/app/soc/cache/sidebar.py	Sun Jan 25 16:28:07 2009 +0000
@@ -0,0 +1,75 @@
+#!/usr/bin/python2.5
+#
+# Copyright 2008 the Melange authors.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+"""Module contains sidebar memcaching functions.
+"""
+
+__authors__ = [
+    '"Sverre Rabbelier" <sverre@rabbelier.nl>',
+  ]
+
+
+from google.appengine.api import memcache
+from google.appengine.api import users
+
+import soc.cache.base
+
+
+def key(user):
+  """Returns the memcache key for the user's sidebar
+  """
+
+  return 'sidebar_for_%s' % repr(user)
+
+
+def get():
+  """Retrieves the sidebar for the specified user from the memcache
+  """
+
+  user = users.get_current_user()
+  return memcache.get(key(user))
+
+
+def put(sidebar):
+  """Sets the sidebar for the specified user in the memcache
+
+  Args:
+    sidebar: the sidebar to be cached
+  """
+
+  # Store sidebar for an hour since new programs might get added
+  # etc. etc.
+  retention = 60*60
+
+  user = users.get_current_user()
+  memcache.add(key(user), sidebar, retention)
+
+
+def flush(user=None):
+  """Removes the sidebar for the current user from the memcache
+
+  Args:
+    user: defaults to the current user if not set
+  """
+
+  if not user:
+    user = users.get_current_user()
+
+  memcache.delete(key(user))
+
+
+# define the cache function
+cache = soc.cache.base.getCacher(get, put)
--- a/app/soc/views/helper/decorators.py	Sun Jan 25 16:25:17 2009 +0000
+++ b/app/soc/views/helper/decorators.py	Sun Jan 25 16:28:07 2009 +0000
@@ -19,6 +19,7 @@
 
 __authors__ = [
   '"Pawel Solyga" <pawel.solyga@gmail.com>',
+  '"Sverre Rabbelier" <sverre@rabbelier.nl>',
   ]
 
 
--- a/app/soc/views/helper/responses.py	Sun Jan 25 16:25:17 2009 +0000
+++ b/app/soc/views/helper/responses.py	Sun Jan 25 16:28:07 2009 +0000
@@ -22,6 +22,7 @@
   '"Pawel Solyga" <pawel.solyga@gmail.com>',
   ]
 
+
 from google.appengine.api import users
 from google.appengine.ext import db
 
@@ -108,6 +109,7 @@
   context['is_debug'] = system.isDebug()
   context['sign_in'] = users.create_login_url(request.path)
   context['sign_out'] = users.create_logout_url(request.path)
+
   context['sidebar_menu_items'] = sidebar.getSidebar()
 
   context['soc_release'] = release.RELEASE_TAG
--- a/app/soc/views/sitemap/sidebar.py	Sun Jan 25 16:25:17 2009 +0000
+++ b/app/soc/views/sitemap/sidebar.py	Sun Jan 25 16:28:07 2009 +0000
@@ -25,6 +25,8 @@
 from soc.views import out_of_band
 from soc.views.helper import access
 
+import soc.cache.sidebar
+
 
 SIDEBAR = []
 SIDEBAR_ACCESS_ARGS = ['SIDEBAR_CALLING']
@@ -40,6 +42,8 @@
   global SIDEBAR
   SIDEBAR.append(callback)
 
+
+@soc.cache.sidebar.cache
 def getSidebar():
   """Constructs a sidebar for the current user.
   """