app/soc/cache/base.py
changeset 987 6fd5c561b446
child 1017 6ad4fdb48840
equal deleted inserted replaced
986:e9611a2288ca 987:6fd5c561b446
       
     1 #!/usr/bin/python2.5
       
     2 #
       
     3 # Copyright 2008 the Melange authors.
       
     4 #
       
     5 # Licensed under the Apache License, Version 2.0 (the "License");
       
     6 # you may not use this file except in compliance with the License.
       
     7 # You may obtain a copy of the License at
       
     8 #
       
     9 #   http://www.apache.org/licenses/LICENSE-2.0
       
    10 #
       
    11 # Unless required by applicable law or agreed to in writing, software
       
    12 # distributed under the License is distributed on an "AS IS" BASIS,
       
    13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       
    14 # See the License for the specific language governing permissions and
       
    15 # limitations under the License.
       
    16 
       
    17 """Module containing some basic caching functions.
       
    18 """
       
    19 
       
    20 __authors__ = [
       
    21     '"Sverre Rabbelier" <sverre@rabbelier.nl>',
       
    22   ]
       
    23 
       
    24 
       
    25 from functools import wraps
       
    26 
       
    27 
       
    28 def getCacher(get, put):
       
    29   """Returns a caching decorator that uses get and put
       
    30   """
       
    31 
       
    32   # TODO(SRabbelier) possibly accept 'key' instead, and define
       
    33   # get and put in terms of key, depends on further usage
       
    34 
       
    35   def cache(func):
       
    36     """Decorator that caches the result from func
       
    37     """
       
    38   
       
    39     @wraps(func)
       
    40     def wrapper(*args, **kwargs):
       
    41       result = get(*args, **kwargs)
       
    42       if result:
       
    43         return result
       
    44 
       
    45       result = func(*args, **kwargs)
       
    46       put(result)
       
    47       return result
       
    48 
       
    49     return wrapper
       
    50 
       
    51   return cache