app/soc/cache/sidebar.py
changeset 987 6fd5c561b446
child 1017 6ad4fdb48840
equal deleted inserted replaced
986:e9611a2288ca 987:6fd5c561b446
       
     1 #!/usr/bin/python2.5
       
     2 #
       
     3 # Copyright 2008 the Melange authors.
       
     4 #
       
     5 # Licensed under the Apache License, Version 2.0 (the "License");
       
     6 # you may not use this file except in compliance with the License.
       
     7 # You may obtain a copy of the License at
       
     8 #
       
     9 #   http://www.apache.org/licenses/LICENSE-2.0
       
    10 #
       
    11 # Unless required by applicable law or agreed to in writing, software
       
    12 # distributed under the License is distributed on an "AS IS" BASIS,
       
    13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       
    14 # See the License for the specific language governing permissions and
       
    15 # limitations under the License.
       
    16 
       
    17 """Module contains sidebar memcaching functions.
       
    18 """
       
    19 
       
    20 __authors__ = [
       
    21     '"Sverre Rabbelier" <sverre@rabbelier.nl>',
       
    22   ]
       
    23 
       
    24 
       
    25 from google.appengine.api import memcache
       
    26 from google.appengine.api import users
       
    27 
       
    28 import soc.cache.base
       
    29 
       
    30 
       
    31 def key(user):
       
    32   """Returns the memcache key for the user's sidebar
       
    33   """
       
    34 
       
    35   return 'sidebar_for_%s' % repr(user)
       
    36 
       
    37 
       
    38 def get():
       
    39   """Retrieves the sidebar for the specified user from the memcache
       
    40   """
       
    41 
       
    42   user = users.get_current_user()
       
    43   return memcache.get(key(user))
       
    44 
       
    45 
       
    46 def put(sidebar):
       
    47   """Sets the sidebar for the specified user in the memcache
       
    48 
       
    49   Args:
       
    50     sidebar: the sidebar to be cached
       
    51   """
       
    52 
       
    53   # Store sidebar for an hour since new programs might get added
       
    54   # etc. etc.
       
    55   retention = 60*60
       
    56 
       
    57   user = users.get_current_user()
       
    58   memcache.add(key(user), sidebar, retention)
       
    59 
       
    60 
       
    61 def flush(user=None):
       
    62   """Removes the sidebar for the current user from the memcache
       
    63 
       
    64   Args:
       
    65     user: defaults to the current user if not set
       
    66   """
       
    67 
       
    68   if not user:
       
    69     user = users.get_current_user()
       
    70 
       
    71   memcache.delete(key(user))
       
    72 
       
    73 
       
    74 # define the cache function
       
    75 cache = soc.cache.base.getCacher(get, put)