2006-02-24 14:07:01 +08:00
|
|
|
"Memcached cache backend"
|
|
|
|
|
2010-02-11 20:06:26 +08:00
|
|
|
import time
|
|
|
|
|
2006-02-24 14:07:01 +08:00
|
|
|
from django.core.cache.backends.base import BaseCache, InvalidCacheBackendError
|
2007-07-16 17:36:10 +08:00
|
|
|
from django.utils.encoding import smart_unicode, smart_str
|
2006-02-24 14:07:01 +08:00
|
|
|
|
|
|
|
try:
|
2007-03-26 07:29:31 +08:00
|
|
|
import cmemcache as memcache
|
2010-03-10 22:02:59 +08:00
|
|
|
import warnings
|
|
|
|
warnings.warn(
|
|
|
|
"Support for the 'cmemcache' library has been deprecated. Please use python-memcached instead.",
|
|
|
|
PendingDeprecationWarning
|
|
|
|
)
|
2006-02-24 14:07:01 +08:00
|
|
|
except ImportError:
|
2007-03-26 07:29:31 +08:00
|
|
|
try:
|
|
|
|
import memcache
|
|
|
|
except:
|
|
|
|
raise InvalidCacheBackendError("Memcached cache backend requires either the 'memcache' or 'cmemcache' library")
|
2006-02-24 14:07:01 +08:00
|
|
|
|
|
|
|
class CacheClass(BaseCache):
|
|
|
|
def __init__(self, server, params):
|
|
|
|
BaseCache.__init__(self, params)
|
|
|
|
self._cache = memcache.Client(server.split(';'))
|
|
|
|
|
2010-02-11 20:06:26 +08:00
|
|
|
def _get_memcache_timeout(self, timeout):
|
|
|
|
"""
|
|
|
|
Memcached deals with long (> 30 days) timeouts in a special
|
|
|
|
way. Call this function to obtain a safe value for your timeout.
|
|
|
|
"""
|
|
|
|
timeout = timeout or self.default_timeout
|
|
|
|
if timeout > 2592000: # 60*60*24*30, 30 days
|
|
|
|
# See http://code.google.com/p/memcached/wiki/FAQ
|
|
|
|
# "You can set expire times up to 30 days in the future. After that
|
|
|
|
# memcached interprets it as a date, and will expire the item after
|
|
|
|
# said date. This is a simple (but obscure) mechanic."
|
|
|
|
#
|
|
|
|
# This means that we have to switch to absolute timestamps.
|
|
|
|
timeout += int(time.time())
|
|
|
|
return timeout
|
|
|
|
|
2007-10-20 23:16:34 +08:00
|
|
|
def add(self, key, value, timeout=0):
|
2008-08-20 04:35:56 +08:00
|
|
|
if isinstance(value, unicode):
|
|
|
|
value = value.encode('utf-8')
|
2010-02-11 20:06:26 +08:00
|
|
|
return self._cache.add(smart_str(key), value, self._get_memcache_timeout(timeout))
|
2007-10-20 23:16:34 +08:00
|
|
|
|
2006-02-24 14:07:01 +08:00
|
|
|
def get(self, key, default=None):
|
2007-07-16 17:36:10 +08:00
|
|
|
val = self._cache.get(smart_str(key))
|
2006-02-24 14:07:01 +08:00
|
|
|
if val is None:
|
|
|
|
return default
|
2010-03-02 04:11:24 +08:00
|
|
|
return val
|
2006-02-24 14:07:01 +08:00
|
|
|
|
|
|
|
def set(self, key, value, timeout=0):
|
2010-02-11 20:06:26 +08:00
|
|
|
self._cache.set(smart_str(key), value, self._get_memcache_timeout(timeout))
|
2006-02-24 14:07:01 +08:00
|
|
|
|
|
|
|
def delete(self, key):
|
2007-07-16 17:36:10 +08:00
|
|
|
self._cache.delete(smart_str(key))
|
2006-02-24 14:07:01 +08:00
|
|
|
|
|
|
|
def get_many(self, keys):
|
2007-07-16 17:36:10 +08:00
|
|
|
return self._cache.get_multi(map(smart_str,keys))
|
2008-08-17 07:35:58 +08:00
|
|
|
|
|
|
|
def close(self, **kwargs):
|
|
|
|
self._cache.disconnect_all()
|
|
|
|
|
2009-03-11 21:27:03 +08:00
|
|
|
def incr(self, key, delta=1):
|
2009-12-14 04:35:06 +08:00
|
|
|
try:
|
|
|
|
val = self._cache.incr(key, delta)
|
|
|
|
|
|
|
|
# python-memcache responds to incr on non-existent keys by
|
|
|
|
# raising a ValueError. Cmemcache returns None. In both
|
|
|
|
# cases, we should raise a ValueError though.
|
|
|
|
except ValueError:
|
|
|
|
val = None
|
|
|
|
if val is None:
|
|
|
|
raise ValueError("Key '%s' not found" % key)
|
|
|
|
|
|
|
|
return val
|
2009-03-11 21:27:03 +08:00
|
|
|
|
|
|
|
def decr(self, key, delta=1):
|
2009-12-14 04:35:06 +08:00
|
|
|
try:
|
|
|
|
val = self._cache.decr(key, delta)
|
|
|
|
|
|
|
|
# python-memcache responds to decr on non-existent keys by
|
|
|
|
# raising a ValueError. Cmemcache returns None. In both
|
|
|
|
# cases, we should raise a ValueError though.
|
|
|
|
except ValueError:
|
|
|
|
val = None
|
|
|
|
if val is None:
|
|
|
|
raise ValueError("Key '%s' not found" % key)
|
|
|
|
return val
|
2010-01-27 16:21:35 +08:00
|
|
|
|
|
|
|
def set_many(self, data, timeout=0):
|
|
|
|
safe_data = {}
|
|
|
|
for key, value in data.items():
|
|
|
|
if isinstance(value, unicode):
|
|
|
|
value = value.encode('utf-8')
|
|
|
|
safe_data[smart_str(key)] = value
|
2010-02-11 20:06:26 +08:00
|
|
|
self._cache.set_multi(safe_data, self._get_memcache_timeout(timeout))
|
2010-01-27 16:21:35 +08:00
|
|
|
|
|
|
|
def delete_many(self, keys):
|
|
|
|
self._cache.delete_multi(map(smart_str, keys))
|
|
|
|
|
|
|
|
def clear(self):
|
|
|
|
self._cache.flush_all()
|