app/soc/cache/home.py
author Sverre Rabbelier <srabbelier@gmail.com>
Fri, 18 Sep 2009 22:08:50 +0200
changeset 2962 05014496b8f9
parent 2879 cb0f9b4646aa
permissions -rw-r--r--
Throw an exception when registering an already registered right
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1419
5d0f80ad7b9f Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     1
#!/usr/bin/python2.5
5d0f80ad7b9f Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     2
#
5d0f80ad7b9f Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     3
# Copyright 2009 the Melange authors.
5d0f80ad7b9f Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     4
#
5d0f80ad7b9f Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     5
# Licensed under the Apache License, Version 2.0 (the "License");
5d0f80ad7b9f Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     6
# you may not use this file except in compliance with the License.
5d0f80ad7b9f Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     7
# You may obtain a copy of the License at
5d0f80ad7b9f Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     8
#
5d0f80ad7b9f Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     9
#   http://www.apache.org/licenses/LICENSE-2.0
5d0f80ad7b9f Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    10
#
5d0f80ad7b9f Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    11
# Unless required by applicable law or agreed to in writing, software
5d0f80ad7b9f Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    12
# distributed under the License is distributed on an "AS IS" BASIS,
5d0f80ad7b9f Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
5d0f80ad7b9f Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    14
# See the License for the specific language governing permissions and
5d0f80ad7b9f Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    15
# limitations under the License.
5d0f80ad7b9f Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    16
2409
5ffa2372f1d6 fix copy/paste fail in homepage caching
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2365
diff changeset
    17
"""Module contains homepage memcaching functions.
1419
5d0f80ad7b9f Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    18
"""
5d0f80ad7b9f Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    19
5d0f80ad7b9f Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    20
__authors__ = [
5d0f80ad7b9f Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    21
    '"Sverre Rabbelier" <sverre@rabbelier.nl>',
5d0f80ad7b9f Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    22
  ]
5d0f80ad7b9f Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    23
5d0f80ad7b9f Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    24
5d0f80ad7b9f Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    25
import logging
5d0f80ad7b9f Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    26
5d0f80ad7b9f Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    27
from google.appengine.api import memcache
1600
0aa3de1b2acc Fix access bug due to forgotten normalization
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1419
diff changeset
    28
0aa3de1b2acc Fix access bug due to forgotten normalization
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1419
diff changeset
    29
from soc.logic import accounts
2879
cb0f9b4646aa Revert "revert wrong head"
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2871
diff changeset
    30
from soc.logic import system
1419
5d0f80ad7b9f Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    31
5d0f80ad7b9f Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    32
import soc.cache.base
5d0f80ad7b9f Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    33
5d0f80ad7b9f Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    34
5d0f80ad7b9f Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    35
def key(entity):
2409
5ffa2372f1d6 fix copy/paste fail in homepage caching
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2365
diff changeset
    36
  """Returns the memcache key for an entities homepage.
1419
5d0f80ad7b9f Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    37
  """
5d0f80ad7b9f Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    38
2879
cb0f9b4646aa Revert "revert wrong head"
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2871
diff changeset
    39
  host = system.getHostname()
cb0f9b4646aa Revert "revert wrong head"
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2871
diff changeset
    40
  version = system.getAppVersion()
cb0f9b4646aa Revert "revert wrong head"
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2871
diff changeset
    41
  kind = entity.kind()
cb0f9b4646aa Revert "revert wrong head"
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2871
diff changeset
    42
  key = entity.key().id_or_name()
cb0f9b4646aa Revert "revert wrong head"
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2871
diff changeset
    43
cb0f9b4646aa Revert "revert wrong head"
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2871
diff changeset
    44
  return 'homepage_for_%s_%s_%s_%s' % (host, version, kind, key)
1419
5d0f80ad7b9f Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    45
5d0f80ad7b9f Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    46
5d0f80ad7b9f Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    47
def get(self, *args, **kwargs):
2409
5ffa2372f1d6 fix copy/paste fail in homepage caching
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2365
diff changeset
    48
  """Retrieves the homepage for the specified entity from the memcache.
1419
5d0f80ad7b9f Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    49
  """
5d0f80ad7b9f Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    50
5d0f80ad7b9f Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    51
  # only cache the page for non-logged-in users
5d0f80ad7b9f Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    52
  # TODO: figure out how to cache everything but the sidebar
1600
0aa3de1b2acc Fix access bug due to forgotten normalization
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1419
diff changeset
    53
  # also, no need to normalize as we don't use it anyway
0aa3de1b2acc Fix access bug due to forgotten normalization
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1419
diff changeset
    54
  if accounts.getCurrentAccount(normalize=False):
1419
5d0f80ad7b9f Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    55
    return (None, None)
5d0f80ad7b9f Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    56
5d0f80ad7b9f Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    57
  entity = self._logic.getFromKeyFields(kwargs)
5d0f80ad7b9f Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    58
5d0f80ad7b9f Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    59
  # if we can't retrieve the entity, leave it to the actual method
5d0f80ad7b9f Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    60
  if not entity:
5d0f80ad7b9f Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    61
    return (None, None)
5d0f80ad7b9f Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    62
5d0f80ad7b9f Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    63
  memcache_key = key(entity)
5d0f80ad7b9f Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    64
  logging.info("Retrieving %s" % memcache_key)
2365
a66e1dd8ced7 Add pylint: disable-msg= where needed in Melange modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2160
diff changeset
    65
  # pylint: disable-msg=E1101
1419
5d0f80ad7b9f Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    66
  return memcache.get(memcache_key), memcache_key
5d0f80ad7b9f Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    67
5d0f80ad7b9f Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    68
def put(result, memcache_key, *args, **kwargs):
2409
5ffa2372f1d6 fix copy/paste fail in homepage caching
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2365
diff changeset
    69
  """Sets the homepage for the specified entity in the memcache.
1419
5d0f80ad7b9f Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    70
5d0f80ad7b9f Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    71
  Args:
2409
5ffa2372f1d6 fix copy/paste fail in homepage caching
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2365
diff changeset
    72
    result: the homepage to be cached
1419
5d0f80ad7b9f Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    73
  """
5d0f80ad7b9f Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    74
5d0f80ad7b9f Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    75
  # no sense in storing anything if we won't query it later on
1600
0aa3de1b2acc Fix access bug due to forgotten normalization
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1419
diff changeset
    76
  # also, no need to normalize as we don't use it anyway
0aa3de1b2acc Fix access bug due to forgotten normalization
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1419
diff changeset
    77
  if accounts.getCurrentAccount(normalize=False):
1419
5d0f80ad7b9f Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    78
    return
5d0f80ad7b9f Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    79
2409
5ffa2372f1d6 fix copy/paste fail in homepage caching
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2365
diff changeset
    80
  # Store homepage for just ten minutes to force a refresh every so often
1419
5d0f80ad7b9f Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    81
  retention = 10*60
5d0f80ad7b9f Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    82
5d0f80ad7b9f Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    83
  logging.info("Setting %s" % memcache_key)
2365
a66e1dd8ced7 Add pylint: disable-msg= where needed in Melange modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2160
diff changeset
    84
  # pylint: disable-msg=E1101
1419
5d0f80ad7b9f Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    85
  memcache.add(memcache_key, result, retention)
5d0f80ad7b9f Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    86
5d0f80ad7b9f Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    87
5d0f80ad7b9f Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    88
def flush(entity):
2409
5ffa2372f1d6 fix copy/paste fail in homepage caching
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2365
diff changeset
    89
  """Removes the homepage for the current entity from the memcache.
1419
5d0f80ad7b9f Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    90
5d0f80ad7b9f Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    91
  Also calls soc.cache.rights.flush for the specified user.
5d0f80ad7b9f Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    92
5d0f80ad7b9f Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    93
  Args:
5d0f80ad7b9f Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    94
    id: defaults to the current account if not set
5d0f80ad7b9f Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    95
  """
5d0f80ad7b9f Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    96
5d0f80ad7b9f Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    97
  memcache_key = key(entity)
5d0f80ad7b9f Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    98
  logging.info("Flushing %s" % memcache_key)
2365
a66e1dd8ced7 Add pylint: disable-msg= where needed in Melange modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2160
diff changeset
    99
  # pylint: disable-msg=E1101
1419
5d0f80ad7b9f Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   100
  memcache.delete(memcache_key)
5d0f80ad7b9f Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   101
5d0f80ad7b9f Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   102
5d0f80ad7b9f Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   103
# define the cache function
5d0f80ad7b9f Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   104
cache = soc.cache.base.getCacher(get, put)