2006-02-24 14:07:01 +08:00
|
|
|
"Base Cache class."
|
|
|
|
|
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
|
|
|
|
|
|
|
class InvalidCacheBackendError(ImproperlyConfigured):
|
|
|
|
pass
|
|
|
|
|
2006-06-08 13:00:13 +08:00
|
|
|
class BaseCache(object):
|
2006-02-24 14:07:01 +08:00
|
|
|
def __init__(self, params):
|
|
|
|
timeout = params.get('timeout', 300)
|
|
|
|
try:
|
|
|
|
timeout = int(timeout)
|
|
|
|
except (ValueError, TypeError):
|
|
|
|
timeout = 300
|
|
|
|
self.default_timeout = timeout
|
|
|
|
|
2007-10-20 23:16:34 +08:00
|
|
|
def add(self, key, value, timeout=None):
|
|
|
|
"""
|
2007-11-30 13:30:43 +08:00
|
|
|
Set a value in the cache if the key does not already exist. If
|
2007-10-20 23:16:34 +08:00
|
|
|
timeout is given, that timeout will be used for the key; otherwise
|
|
|
|
the default cache timeout will be used.
|
2008-08-10 11:52:21 +08:00
|
|
|
|
|
|
|
Returns True if the value was stored, False otherwise.
|
2007-10-20 23:16:34 +08:00
|
|
|
"""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2006-02-24 14:07:01 +08:00
|
|
|
def get(self, key, default=None):
|
|
|
|
"""
|
2007-11-30 13:30:43 +08:00
|
|
|
Fetch a given key from the cache. If the key does not exist, return
|
2006-02-24 14:07:01 +08:00
|
|
|
default, which itself defaults to None.
|
|
|
|
"""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def set(self, key, value, timeout=None):
|
|
|
|
"""
|
2007-11-30 13:30:43 +08:00
|
|
|
Set a value in the cache. If timeout is given, that timeout will be
|
2006-02-24 14:07:01 +08:00
|
|
|
used for the key; otherwise the default cache timeout will be used.
|
|
|
|
"""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def delete(self, key):
|
|
|
|
"""
|
|
|
|
Delete a key from the cache, failing silently.
|
|
|
|
"""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def get_many(self, keys):
|
|
|
|
"""
|
2007-11-30 13:30:43 +08:00
|
|
|
Fetch a bunch of keys from the cache. For certain backends (memcached,
|
2006-02-24 14:07:01 +08:00
|
|
|
pgsql) this can be *much* faster when fetching multiple values.
|
|
|
|
|
2007-11-30 13:30:43 +08:00
|
|
|
Returns a dict mapping each key in keys to its value. If the given
|
2006-02-24 14:07:01 +08:00
|
|
|
key is missing, it will be missing from the response dict.
|
|
|
|
"""
|
|
|
|
d = {}
|
|
|
|
for k in keys:
|
|
|
|
val = self.get(k)
|
|
|
|
if val is not None:
|
|
|
|
d[k] = val
|
|
|
|
return d
|
|
|
|
|
|
|
|
def has_key(self, key):
|
|
|
|
"""
|
|
|
|
Returns True if the key is in the cache and has not expired.
|
|
|
|
"""
|
|
|
|
return self.get(key) is not None
|
2007-05-08 12:13:46 +08:00
|
|
|
|
2008-07-26 11:58:31 +08:00
|
|
|
def __contains__(self, key):
|
|
|
|
"""
|
|
|
|
Returns True if the key is in the cache and has not expired.
|
|
|
|
"""
|
|
|
|
# This is a separate method, rather than just a copy of has_key(),
|
|
|
|
# so that it always has the same functionality as has_key(), even
|
|
|
|
# if a subclass overrides it.
|
|
|
|
return self.has_key(key)
|