app/soc/cache/logic.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:
1795
336c35c82b87 Added query caching
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     1
#!/usr/bin/python2.5
336c35c82b87 Added query caching
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     2
#
336c35c82b87 Added query caching
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     3
# Copyright 2009 the Melange authors.
336c35c82b87 Added query caching
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     4
#
336c35c82b87 Added query caching
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     5
# Licensed under the Apache License, Version 2.0 (the "License");
336c35c82b87 Added query caching
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     6
# you may not use this file except in compliance with the License.
336c35c82b87 Added query caching
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     7
# You may obtain a copy of the License at
336c35c82b87 Added query caching
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     8
#
336c35c82b87 Added query caching
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     9
#   http://www.apache.org/licenses/LICENSE-2.0
336c35c82b87 Added query caching
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    10
#
336c35c82b87 Added query caching
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    11
# Unless required by applicable law or agreed to in writing, software
336c35c82b87 Added query caching
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    12
# distributed under the License is distributed on an "AS IS" BASIS,
336c35c82b87 Added query caching
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
336c35c82b87 Added query caching
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    14
# See the License for the specific language governing permissions and
336c35c82b87 Added query caching
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    15
# limitations under the License.
336c35c82b87 Added query caching
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    16
336c35c82b87 Added query caching
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    17
"""Module contains logic memcaching functions.
336c35c82b87 Added query caching
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    18
"""
336c35c82b87 Added query caching
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    19
336c35c82b87 Added query caching
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    20
__authors__ = [
336c35c82b87 Added query caching
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    21
    '"Sverre Rabbelier" <sverre@rabbelier.nl>',
336c35c82b87 Added query caching
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    22
  ]
336c35c82b87 Added query caching
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    23
336c35c82b87 Added query caching
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    24
336c35c82b87 Added query caching
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    25
from google.appengine.api import memcache
1958
8e8f45dd46e3 Made _getPickData more generic by supporting ordening
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1795
diff changeset
    26
from google.appengine.ext import db
1795
336c35c82b87 Added query caching
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    27
336c35c82b87 Added query caching
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    28
import soc.cache.base
336c35c82b87 Added query caching
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    29
336c35c82b87 Added query caching
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    30
1958
8e8f45dd46e3 Made _getPickData more generic by supporting ordening
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1795
diff changeset
    31
def key(model, filter, order):
1795
336c35c82b87 Added query caching
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    32
  """Returns the memcache key for this query.
336c35c82b87 Added query caching
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    33
  """
336c35c82b87 Added query caching
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    34
1958
8e8f45dd46e3 Made _getPickData more generic by supporting ordening
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1795
diff changeset
    35
  new_filter = {}
8e8f45dd46e3 Made _getPickData more generic by supporting ordening
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1795
diff changeset
    36
2070
9555be8634a3 Remove unused imports soc.cache.base module and rename key variable to filter_key in soc.views.sitemap modules (access to outer scope names).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1958
diff changeset
    37
  for filter_key, value in filter.iteritems():
2187
e4172c7792f5 Fix too long line in soc.cache.logic module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2160
diff changeset
    38
    if isinstance(value, db.Model):
e4172c7792f5 Fix too long line in soc.cache.logic module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2160
diff changeset
    39
      new_value = value.key().id_or_name()
e4172c7792f5 Fix too long line in soc.cache.logic module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2160
diff changeset
    40
    else:
e4172c7792f5 Fix too long line in soc.cache.logic module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2160
diff changeset
    41
      new_value = value
2070
9555be8634a3 Remove unused imports soc.cache.base module and rename key variable to filter_key in soc.views.sitemap modules (access to outer scope names).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1958
diff changeset
    42
    new_filter[filter_key] = new_value
1958
8e8f45dd46e3 Made _getPickData more generic by supporting ordening
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1795
diff changeset
    43
8e8f45dd46e3 Made _getPickData more generic by supporting ordening
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1795
diff changeset
    44
  return 'query_for_%(kind)s_%(filter)s_%(order)s' % {
8e8f45dd46e3 Made _getPickData more generic by supporting ordening
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1795
diff changeset
    45
      'kind': repr(model.kind()),
8e8f45dd46e3 Made _getPickData more generic by supporting ordening
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1795
diff changeset
    46
      'filter': repr(new_filter),
8e8f45dd46e3 Made _getPickData more generic by supporting ordening
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1795
diff changeset
    47
      'order': repr(order),
8e8f45dd46e3 Made _getPickData more generic by supporting ordening
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1795
diff changeset
    48
      }
1795
336c35c82b87 Added query caching
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    49
336c35c82b87 Added query caching
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    50
1958
8e8f45dd46e3 Made _getPickData more generic by supporting ordening
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1795
diff changeset
    51
def get(model, filter, order, *args, **kwargs):
1795
336c35c82b87 Added query caching
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    52
  """Retrieves the data for the specified query from the memcache.
336c35c82b87 Added query caching
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    53
  """
336c35c82b87 Added query caching
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    54
1958
8e8f45dd46e3 Made _getPickData more generic by supporting ordening
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1795
diff changeset
    55
  memcache_key = key(model, filter, order)
2365
a66e1dd8ced7 Add pylint: disable-msg= where needed in Melange modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2187
diff changeset
    56
  # pylint: disable-msg=E1101
1795
336c35c82b87 Added query caching
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    57
  return memcache.get(memcache_key), memcache_key
336c35c82b87 Added query caching
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    58
336c35c82b87 Added query caching
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    59
336c35c82b87 Added query caching
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    60
def put(data, memcache_key, *args, **kwargs):
336c35c82b87 Added query caching
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    61
  """Sets the data for the specified query in the memcache.
336c35c82b87 Added query caching
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    62
336c35c82b87 Added query caching
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    63
  Args:
336c35c82b87 Added query caching
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    64
    data: the data to be cached
336c35c82b87 Added query caching
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    65
  """
336c35c82b87 Added query caching
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    66
336c35c82b87 Added query caching
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    67
  # Store data for fifteen minutes to force a refresh every so often
336c35c82b87 Added query caching
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    68
  retention = 15*60
2365
a66e1dd8ced7 Add pylint: disable-msg= where needed in Melange modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2187
diff changeset
    69
  # pylint: disable-msg=E1101
1795
336c35c82b87 Added query caching
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    70
  memcache.add(memcache_key, data, retention)
336c35c82b87 Added query caching
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    71
336c35c82b87 Added query caching
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    72
336c35c82b87 Added query caching
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    73
def flush(model, filter):
336c35c82b87 Added query caching
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    74
  """Removes the data for the current user from the memcache.
336c35c82b87 Added query caching
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    75
  """
336c35c82b87 Added query caching
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    76
336c35c82b87 Added query caching
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    77
  memcache_key = key(model, filter)
2365
a66e1dd8ced7 Add pylint: disable-msg= where needed in Melange modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2187
diff changeset
    78
  # pylint: disable-msg=E1101
1795
336c35c82b87 Added query caching
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    79
  memcache.delete(memcache_key)
336c35c82b87 Added query caching
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    80
336c35c82b87 Added query caching
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    81
336c35c82b87 Added query caching
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    82
# define the cache function
336c35c82b87 Added query caching
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    83
cache = soc.cache.base.getCacher(get, put)