app/soc/cache/sidebar.py
author Lennard de Rijk <ljvderijk@gmail.com>
Mon, 16 Mar 2009 15:33:27 +0000
changeset 1889 4356a9201d20
parent 1600 0aa3de1b2acc
child 2365 a66e1dd8ced7
permissions -rw-r--r--
Use site_name if the system sends a message. Addresses comments on r2564. Patch by: Lennard de Rijk Reviewed by: to-be-reviewed
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
987
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     1
#!/usr/bin/python2.5
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     2
#
1307
091a21cf3627 Update the copyright notice for 2009.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1033
diff changeset
     3
# Copyright 2009 the Melange authors.
987
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     4
#
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     5
# Licensed under the Apache License, Version 2.0 (the "License");
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     6
# you may not use this file except in compliance with the License.
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     7
# You may obtain a copy of the License at
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     8
#
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     9
#   http://www.apache.org/licenses/LICENSE-2.0
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    10
#
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    11
# Unless required by applicable law or agreed to in writing, software
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    12
# distributed under the License is distributed on an "AS IS" BASIS,
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    14
# See the License for the specific language governing permissions and
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    15
# limitations under the License.
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    16
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    17
"""Module contains sidebar memcaching functions.
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    18
"""
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    19
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    20
__authors__ = [
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    21
    '"Sverre Rabbelier" <sverre@rabbelier.nl>',
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    22
  ]
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    23
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    24
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    25
from google.appengine.api import memcache
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    26
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    27
import soc.cache.base
1033
71ff49dbb4b6 Also flush the user's ACL's when flushing the sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1029
diff changeset
    28
import soc.cache.rights
1600
0aa3de1b2acc Fix access bug due to forgotten normalization
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1476
diff changeset
    29
import soc.logic.accounts
987
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    30
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    31
1017
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 987
diff changeset
    32
def key(id):
1029
a312e7007811 Style fixes in soc.cache modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1020
diff changeset
    33
  """Returns the memcache key for the user's sidebar.
987
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    34
  """
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    35
1017
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 987
diff changeset
    36
  return 'sidebar_for_%s' % repr(id)
987
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    37
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    38
1476
ef8e8de75ebb Be more lenient in what we accept in cache.get() and cache.put()
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1418
diff changeset
    39
def get(id, *args, **kwargs):
1029
a312e7007811 Style fixes in soc.cache modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1020
diff changeset
    40
  """Retrieves the sidebar for the specified user from the memcache.
987
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    41
  """
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    42
1017
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 987
diff changeset
    43
  memcache_key = key(id)
1418
50e989482d1b Compute the memcache_key only in get()
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1307
diff changeset
    44
  return memcache.get(memcache_key), memcache_key
987
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    45
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    46
1476
ef8e8de75ebb Be more lenient in what we accept in cache.get() and cache.put()
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1418
diff changeset
    47
def put(sidebar, memcache_key, *args, **kwargs):
1029
a312e7007811 Style fixes in soc.cache modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1020
diff changeset
    48
  """Sets the sidebar for the specified user in the memcache.
987
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    49
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    50
  Args:
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    51
    sidebar: the sidebar to be cached
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    52
  """
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    53
1418
50e989482d1b Compute the memcache_key only in get()
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1307
diff changeset
    54
  # Store sidebar for just three minutes to force a refresh every so often
1020
846fe3d821fb Reduce caching time of sidebar to 3 minutes
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1017
diff changeset
    55
  retention = 3*60
987
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    56
1017
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 987
diff changeset
    57
  memcache.add(memcache_key, sidebar, retention)
987
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    58
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    59
1033
71ff49dbb4b6 Also flush the user's ACL's when flushing the sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1029
diff changeset
    60
def flush(id=None):
1029
a312e7007811 Style fixes in soc.cache modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1020
diff changeset
    61
  """Removes the sidebar for the current user from the memcache.
987
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    62
1033
71ff49dbb4b6 Also flush the user's ACL's when flushing the sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1029
diff changeset
    63
  Also calls soc.cache.rights.flush for the specified user.
71ff49dbb4b6 Also flush the user's ACL's when flushing the sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1029
diff changeset
    64
987
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    65
  Args:
1033
71ff49dbb4b6 Also flush the user's ACL's when flushing the sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1029
diff changeset
    66
    id: defaults to the current account if not set
987
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    67
  """
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    68
1033
71ff49dbb4b6 Also flush the user's ACL's when flushing the sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1029
diff changeset
    69
  if not id:
1600
0aa3de1b2acc Fix access bug due to forgotten normalization
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1476
diff changeset
    70
    id = soc.logic.accounts.getCurrentAccount()
987
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    71
1033
71ff49dbb4b6 Also flush the user's ACL's when flushing the sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1029
diff changeset
    72
  memcache_key = key(id)
1017
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 987
diff changeset
    73
  memcache.delete(memcache_key)
1033
71ff49dbb4b6 Also flush the user's ACL's when flushing the sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1029
diff changeset
    74
  soc.cache.rights.flush(id)
987
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    75
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    76
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    77
# define the cache function
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    78
cache = soc.cache.base.getCacher(get, put)