app/soc/cache/sidebar.py
author Lennard de Rijk <ljvderijk@gmail.com>
Tue, 30 Jun 2009 14:56:48 +0200
changeset 2464 64b241a237b2
parent 2365 a66e1dd8ced7
permissions -rw-r--r--
Merge params from inheriting Survey views so functionality can be reused.
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)
2365
a66e1dd8ced7 Add pylint: disable-msg= where needed in Melange modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1600
diff changeset
    44
  # pylint: disable-msg=E1101
1418
50e989482d1b Compute the memcache_key only in get()
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1307
diff changeset
    45
  return memcache.get(memcache_key), memcache_key
987
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    46
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    47
1476
ef8e8de75ebb Be more lenient in what we accept in cache.get() and cache.put()
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1418
diff changeset
    48
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
    49
  """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
    50
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    51
  Args:
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    52
    sidebar: the sidebar to be cached
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    53
  """
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    54
1418
50e989482d1b Compute the memcache_key only in get()
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1307
diff changeset
    55
  # 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
    56
  retention = 3*60
2365
a66e1dd8ced7 Add pylint: disable-msg= where needed in Melange modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1600
diff changeset
    57
  # pylint: disable-msg=E1101
1017
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 987
diff changeset
    58
  memcache.add(memcache_key, sidebar, retention)
987
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    59
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    60
1033
71ff49dbb4b6 Also flush the user's ACL's when flushing the sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1029
diff changeset
    61
def flush(id=None):
1029
a312e7007811 Style fixes in soc.cache modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1020
diff changeset
    62
  """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
    63
1033
71ff49dbb4b6 Also flush the user's ACL's when flushing the sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1029
diff changeset
    64
  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
    65
987
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    66
  Args:
1033
71ff49dbb4b6 Also flush the user's ACL's when flushing the sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1029
diff changeset
    67
    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
    68
  """
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    69
1033
71ff49dbb4b6 Also flush the user's ACL's when flushing the sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1029
diff changeset
    70
  if not id:
1600
0aa3de1b2acc Fix access bug due to forgotten normalization
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1476
diff changeset
    71
    id = soc.logic.accounts.getCurrentAccount()
987
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    72
1033
71ff49dbb4b6 Also flush the user's ACL's when flushing the sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1029
diff changeset
    73
  memcache_key = key(id)
2365
a66e1dd8ced7 Add pylint: disable-msg= where needed in Melange modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1600
diff changeset
    74
  # pylint: disable-msg=E1101
1017
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 987
diff changeset
    75
  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
    76
  soc.cache.rights.flush(id)
987
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    77
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    78
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    79
# define the cache function
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    80
cache = soc.cache.base.getCacher(get, put)