app/soc/cache/sidebar.py
changeset 1017 6ad4fdb48840
parent 987 6fd5c561b446
child 1020 846fe3d821fb
equal deleted inserted replaced
1016:15a2f644725f 1017:6ad4fdb48840
    26 from google.appengine.api import users
    26 from google.appengine.api import users
    27 
    27 
    28 import soc.cache.base
    28 import soc.cache.base
    29 
    29 
    30 
    30 
    31 def key(user):
    31 def key(id):
    32   """Returns the memcache key for the user's sidebar
    32   """Returns the memcache key for the user's sidebar
    33   """
    33   """
    34 
    34 
    35   return 'sidebar_for_%s' % repr(user)
    35   return 'sidebar_for_%s' % repr(id)
    36 
    36 
    37 
    37 
    38 def get():
    38 def get(id, user):
    39   """Retrieves the sidebar for the specified user from the memcache
    39   """Retrieves the sidebar for the specified user from the memcache
    40   """
    40   """
    41 
    41 
    42   user = users.get_current_user()
    42   memcache_key = key(id)
    43   return memcache.get(key(user))
    43   return memcache.get(memcache_key)
    44 
    44 
    45 
    45 
    46 def put(sidebar):
    46 def put(sidebar, id, user):
    47   """Sets the sidebar for the specified user in the memcache
    47   """Sets the sidebar for the specified user in the memcache
    48 
    48 
    49   Args:
    49   Args:
    50     sidebar: the sidebar to be cached
    50     sidebar: the sidebar to be cached
    51   """
    51   """
    52 
    52 
    53   # Store sidebar for an hour since new programs might get added
    53   # Store sidebar for ten minutes since new programs might get added
    54   # etc. etc.
    54   retention = 10*60
    55   retention = 60*60
       
    56 
    55 
    57   user = users.get_current_user()
    56   memcache_key = key(id)
    58   memcache.add(key(user), sidebar, retention)
    57   memcache.add(memcache_key, sidebar, retention)
    59 
    58 
    60 
    59 
    61 def flush(user=None):
    60 def flush(user=None):
    62   """Removes the sidebar for the current user from the memcache
    61   """Removes the sidebar for the current user from the memcache
    63 
    62 
    66   """
    65   """
    67 
    66 
    68   if not user:
    67   if not user:
    69     user = users.get_current_user()
    68     user = users.get_current_user()
    70 
    69 
    71   memcache.delete(key(user))
    70   memcache_key = key(user)
       
    71   memcache.delete(memcache_key)
    72 
    72 
    73 
    73 
    74 # define the cache function
    74 # define the cache function
    75 cache = soc.cache.base.getCacher(get, put)
    75 cache = soc.cache.base.getCacher(get, put)