app/soc/cache/base.py
author Pawel Solyga <Pawel.Solyga@gmail.com>
Fri, 03 Apr 2009 17:06:27 +0000
changeset 2065 edac5ddc8ef0
parent 1829 c9f30de8804f
child 2070 9555be8634a3
permissions -rw-r--r--
Add license file and docstring to __init__.py files across different modules within Melange. Patch by: Pawel Solyga 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: 1029
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 containing some basic caching 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 functools import wraps
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
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    28
def getCacher(get, put):
1029
a312e7007811 Style fixes in soc.cache modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1017
diff changeset
    29
  """Returns a caching decorator that uses get and put.
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
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    32
  # TODO(SRabbelier) possibly accept 'key' instead, and define
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    33
  # get and put in terms of key, depends on further usage
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
  def cache(func):
1029
a312e7007811 Style fixes in soc.cache modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1017
diff changeset
    36
    """Decorator that caches the result from func.
987
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    37
    """
1829
c9f30de8804f Fixed whitespace in cache/base.py
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1418
diff changeset
    38
987
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    39
    @wraps(func)
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    40
    def wrapper(*args, **kwargs):
1418
50e989482d1b Compute the memcache_key only in get()
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1307
diff changeset
    41
      result, key = get(*args, **kwargs)
987
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    42
      if result:
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    43
        return result
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    44
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    45
      result = func(*args, **kwargs)
1418
50e989482d1b Compute the memcache_key only in get()
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1307
diff changeset
    46
50e989482d1b Compute the memcache_key only in get()
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1307
diff changeset
    47
      if key:
50e989482d1b Compute the memcache_key only in get()
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1307
diff changeset
    48
        put(result, key, *args, **kwargs)
50e989482d1b Compute the memcache_key only in get()
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1307
diff changeset
    49
987
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    50
      return result
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    51
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    52
    return wrapper
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
  return cache