web/lib/django/core/cache/backends/base.py
changeset 0 0d40e90630ef
child 29 cc9b7e14412b
equal deleted inserted replaced
-1:000000000000 0:0d40e90630ef
       
     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         Returns True if the value was stored, False otherwise.
       
    24         """
       
    25         raise NotImplementedError
       
    26 
       
    27     def get(self, key, default=None):
       
    28         """
       
    29         Fetch a given key from the cache. If the key does not exist, return
       
    30         default, which itself defaults to None.
       
    31         """
       
    32         raise NotImplementedError
       
    33 
       
    34     def set(self, key, value, timeout=None):
       
    35         """
       
    36         Set a value in the cache. If timeout is given, that timeout will be
       
    37         used for the key; otherwise the default cache timeout will be used.
       
    38         """
       
    39         raise NotImplementedError
       
    40 
       
    41     def delete(self, key):
       
    42         """
       
    43         Delete a key from the cache, failing silently.
       
    44         """
       
    45         raise NotImplementedError
       
    46 
       
    47     def get_many(self, keys):
       
    48         """
       
    49         Fetch a bunch of keys from the cache. For certain backends (memcached,
       
    50         pgsql) this can be *much* faster when fetching multiple values.
       
    51 
       
    52         Returns a dict mapping each key in keys to its value. If the given
       
    53         key is missing, it will be missing from the response dict.
       
    54         """
       
    55         d = {}
       
    56         for k in keys:
       
    57             val = self.get(k)
       
    58             if val is not None:
       
    59                 d[k] = val
       
    60         return d
       
    61 
       
    62     def has_key(self, key):
       
    63         """
       
    64         Returns True if the key is in the cache and has not expired.
       
    65         """
       
    66         return self.get(key) is not None
       
    67 
       
    68     def incr(self, key, delta=1):
       
    69         """
       
    70         Add delta to value in the cache. If the key does not exist, raise a
       
    71         ValueError exception.
       
    72         """
       
    73         if key not in self:
       
    74             raise ValueError, "Key '%s' not found" % key
       
    75         new_value = self.get(key) + delta
       
    76         self.set(key, new_value)
       
    77         return new_value
       
    78 
       
    79     def decr(self, key, delta=1):
       
    80         """
       
    81         Subtract delta from value in the cache. If the key does not exist, raise
       
    82         a ValueError exception.
       
    83         """
       
    84         return self.incr(key, -delta)
       
    85 
       
    86     def __contains__(self, key):
       
    87         """
       
    88         Returns True if the key is in the cache and has not expired.
       
    89         """
       
    90         # This is a separate method, rather than just a copy of has_key(),
       
    91         # so that it always has the same functionality as has_key(), even
       
    92         # if a subclass overrides it.
       
    93         return self.has_key(key)