app/django/core/cache/backends/base.py
changeset 54 03e267d67478
child 323 ff1a9aa48cfd
equal deleted inserted replaced
53:57b4279d8c4e 54:03e267d67478
       
     1 "Base Cache class."
       
     2 
       
     3 from django.core.exceptions import ImproperlyConfigured
       
     4 
       
     5 class InvalidCacheBackendError(ImproperlyConfigured):
       
     6     pass
       
     7 
       
     8 class BaseCache(object):
       
     9     def __init__(self, params):
       
    10         timeout = params.get('timeout', 300)
       
    11         try:
       
    12             timeout = int(timeout)
       
    13         except (ValueError, TypeError):
       
    14             timeout = 300
       
    15         self.default_timeout = timeout
       
    16 
       
    17     def add(self, key, value, timeout=None):
       
    18         """
       
    19         Set a value in the cache if the key does not already exist. If
       
    20         timeout is given, that timeout will be used for the key; otherwise
       
    21         the default cache timeout will be used.
       
    22         """
       
    23         raise NotImplementedError
       
    24 
       
    25     def get(self, key, default=None):
       
    26         """
       
    27         Fetch a given key from the cache. If the key does not exist, return
       
    28         default, which itself defaults to None.
       
    29         """
       
    30         raise NotImplementedError
       
    31 
       
    32     def set(self, key, value, timeout=None):
       
    33         """
       
    34         Set a value in the cache. If timeout is given, that timeout will be
       
    35         used for the key; otherwise the default cache timeout will be used.
       
    36         """
       
    37         raise NotImplementedError
       
    38 
       
    39     def delete(self, key):
       
    40         """
       
    41         Delete a key from the cache, failing silently.
       
    42         """
       
    43         raise NotImplementedError
       
    44 
       
    45     def get_many(self, keys):
       
    46         """
       
    47         Fetch a bunch of keys from the cache. For certain backends (memcached,
       
    48         pgsql) this can be *much* faster when fetching multiple values.
       
    49 
       
    50         Returns a dict mapping each key in keys to its value. If the given
       
    51         key is missing, it will be missing from the response dict.
       
    52         """
       
    53         d = {}
       
    54         for k in keys:
       
    55             val = self.get(k)
       
    56             if val is not None:
       
    57                 d[k] = val
       
    58         return d
       
    59 
       
    60     def has_key(self, key):
       
    61         """
       
    62         Returns True if the key is in the cache and has not expired.
       
    63         """
       
    64         return self.get(key) is not None
       
    65 
       
    66     __contains__ = has_key