2006-02-24 14:07:01 +08:00
|
|
|
"Memcached cache backend"
|
|
|
|
|
2010-02-11 20:06:26 +08:00
|
|
|
import time
|
2013-02-24 20:36:04 +08:00
|
|
|
import pickle
|
2010-12-21 23:19:19 +08:00
|
|
|
from threading import local
|
2010-02-11 20:06:26 +08:00
|
|
|
|
2013-05-18 18:54:59 +08:00
|
|
|
from django.core.cache.backends.base import BaseCache, DEFAULT_TIMEOUT
|
2010-12-21 23:19:19 +08:00
|
|
|
|
2012-07-20 20:22:00 +08:00
|
|
|
from django.utils import six
|
2012-08-30 04:40:51 +08:00
|
|
|
from django.utils.encoding import force_str
|
2012-07-20 20:22:00 +08:00
|
|
|
|
2010-12-21 23:19:19 +08:00
|
|
|
class BaseMemcachedCache(BaseCache):
|
|
|
|
def __init__(self, server, params, library, value_not_found_exception):
|
|
|
|
super(BaseMemcachedCache, self).__init__(params)
|
2012-07-20 20:22:00 +08:00
|
|
|
if isinstance(server, six.string_types):
|
2010-12-21 23:19:19 +08:00
|
|
|
self._servers = server.split(';')
|
|
|
|
else:
|
|
|
|
self._servers = server
|
|
|
|
|
|
|
|
# The exception type to catch from the underlying library for a key
|
|
|
|
# that was not found. This is a ValueError for python-memcache,
|
|
|
|
# pylibmc.NotFound for pylibmc, and cmemcache will return None without
|
|
|
|
# raising an exception.
|
|
|
|
self.LibraryValueNotFoundException = value_not_found_exception
|
|
|
|
|
|
|
|
self._lib = library
|
|
|
|
self._options = params.get('OPTIONS', None)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def _cache(self):
|
|
|
|
"""
|
|
|
|
Implements transparent thread-safe access to a memcached client.
|
|
|
|
"""
|
2011-02-19 15:40:09 +08:00
|
|
|
if getattr(self, '_client', None) is None:
|
|
|
|
self._client = self._lib.Client(self._servers)
|
|
|
|
|
|
|
|
return self._client
|
2006-02-24 14:07:01 +08:00
|
|
|
|
2013-05-18 18:54:59 +08:00
|
|
|
def _get_memcache_timeout(self, timeout=DEFAULT_TIMEOUT):
|
2010-02-11 20:06:26 +08:00
|
|
|
"""
|
|
|
|
Memcached deals with long (> 30 days) timeouts in a special
|
|
|
|
way. Call this function to obtain a safe value for your timeout.
|
|
|
|
"""
|
2013-05-18 18:54:59 +08:00
|
|
|
if timeout == DEFAULT_TIMEOUT:
|
|
|
|
return self.default_timeout
|
|
|
|
|
|
|
|
if timeout is None:
|
|
|
|
# Using 0 in memcache sets a non-expiring timeout.
|
|
|
|
return 0
|
|
|
|
elif int(timeout) == 0:
|
|
|
|
# Other cache backends treat 0 as set-and-expire. To achieve this
|
|
|
|
# in memcache backends, a negative timeout must be passed.
|
|
|
|
timeout = -1
|
|
|
|
|
2010-02-11 20:06:26 +08:00
|
|
|
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())
|
2011-07-29 17:39:23 +08:00
|
|
|
return int(timeout)
|
2010-02-11 20:06:26 +08:00
|
|
|
|
2012-08-15 22:56:33 +08:00
|
|
|
def make_key(self, key, version=None):
|
|
|
|
# Python 2 memcache requires the key to be a byte string.
|
2012-08-30 04:40:51 +08:00
|
|
|
return force_str(super(BaseMemcachedCache, self).make_key(key, version))
|
2012-08-15 22:56:33 +08:00
|
|
|
|
2013-05-18 18:54:59 +08:00
|
|
|
def add(self, key, value, timeout=DEFAULT_TIMEOUT, version=None):
|
2010-11-19 23:39:35 +08:00
|
|
|
key = self.make_key(key, version=version)
|
|
|
|
return self._cache.add(key, value, self._get_memcache_timeout(timeout))
|
2007-10-20 23:16:34 +08:00
|
|
|
|
2010-11-19 23:39:35 +08:00
|
|
|
def get(self, key, default=None, version=None):
|
|
|
|
key = self.make_key(key, version=version)
|
|
|
|
val = self._cache.get(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
|
|
|
|
2013-05-18 18:54:59 +08:00
|
|
|
def set(self, key, value, timeout=DEFAULT_TIMEOUT, version=None):
|
2010-11-19 23:39:35 +08:00
|
|
|
key = self.make_key(key, version=version)
|
|
|
|
self._cache.set(key, value, self._get_memcache_timeout(timeout))
|
|
|
|
|
|
|
|
def delete(self, key, version=None):
|
|
|
|
key = self.make_key(key, version=version)
|
|
|
|
self._cache.delete(key)
|
|
|
|
|
|
|
|
def get_many(self, keys, version=None):
|
2013-07-09 23:55:45 +08:00
|
|
|
new_keys = [self.make_key(x, version=version) for x in keys]
|
2010-11-19 23:39:35 +08:00
|
|
|
ret = self._cache.get_multi(new_keys)
|
|
|
|
if ret:
|
|
|
|
_ = {}
|
|
|
|
m = dict(zip(new_keys, keys))
|
|
|
|
for k, v in ret.items():
|
|
|
|
_[m[k]] = v
|
|
|
|
ret = _
|
|
|
|
return ret
|
2008-08-17 07:35:58 +08:00
|
|
|
|
|
|
|
def close(self, **kwargs):
|
|
|
|
self._cache.disconnect_all()
|
|
|
|
|
2010-11-19 23:39:35 +08:00
|
|
|
def incr(self, key, delta=1, version=None):
|
|
|
|
key = self.make_key(key, version=version)
|
2012-11-06 19:19:14 +08:00
|
|
|
# memcached doesn't support a negative delta
|
|
|
|
if delta < 0:
|
|
|
|
return self._cache.decr(key, -delta)
|
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
|
2010-12-21 23:19:19 +08:00
|
|
|
# raising a ValueError, pylibmc by raising a pylibmc.NotFound
|
|
|
|
# and Cmemcache returns None. In all cases,
|
|
|
|
# we should raise a ValueError though.
|
|
|
|
except self.LibraryValueNotFoundException:
|
2009-12-14 04:35:06 +08:00
|
|
|
val = None
|
|
|
|
if val is None:
|
|
|
|
raise ValueError("Key '%s' not found" % key)
|
|
|
|
return val
|
2009-03-11 21:27:03 +08:00
|
|
|
|
2010-11-19 23:39:35 +08:00
|
|
|
def decr(self, key, delta=1, version=None):
|
|
|
|
key = self.make_key(key, version=version)
|
2012-11-06 19:19:14 +08:00
|
|
|
# memcached doesn't support a negative delta
|
|
|
|
if delta < 0:
|
|
|
|
return self._cache.incr(key, -delta)
|
2009-12-14 04:35:06 +08:00
|
|
|
try:
|
|
|
|
val = self._cache.decr(key, delta)
|
|
|
|
|
2010-12-21 23:19:19 +08:00
|
|
|
# python-memcache responds to incr on non-existent keys by
|
|
|
|
# raising a ValueError, pylibmc by raising a pylibmc.NotFound
|
|
|
|
# and Cmemcache returns None. In all cases,
|
|
|
|
# we should raise a ValueError though.
|
|
|
|
except self.LibraryValueNotFoundException:
|
2009-12-14 04:35:06 +08:00
|
|
|
val = None
|
|
|
|
if val is None:
|
|
|
|
raise ValueError("Key '%s' not found" % key)
|
|
|
|
return val
|
2010-01-27 16:21:35 +08:00
|
|
|
|
2013-05-18 18:54:59 +08:00
|
|
|
def set_many(self, data, timeout=DEFAULT_TIMEOUT, version=None):
|
2010-01-27 16:21:35 +08:00
|
|
|
safe_data = {}
|
|
|
|
for key, value in data.items():
|
2010-11-19 23:39:35 +08:00
|
|
|
key = self.make_key(key, version=version)
|
|
|
|
safe_data[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
|
|
|
|
2010-11-19 23:39:35 +08:00
|
|
|
def delete_many(self, keys, version=None):
|
|
|
|
l = lambda x: self.make_key(x, version=version)
|
|
|
|
self._cache.delete_multi(map(l, keys))
|
2010-01-27 16:21:35 +08:00
|
|
|
|
|
|
|
def clear(self):
|
|
|
|
self._cache.flush_all()
|
2010-12-21 23:19:19 +08:00
|
|
|
|
|
|
|
class MemcachedCache(BaseMemcachedCache):
|
|
|
|
"An implementation of a cache binding using python-memcached"
|
|
|
|
def __init__(self, server, params):
|
|
|
|
import memcache
|
|
|
|
super(MemcachedCache, self).__init__(server, params,
|
|
|
|
library=memcache,
|
|
|
|
value_not_found_exception=ValueError)
|
|
|
|
|
2013-02-24 20:36:04 +08:00
|
|
|
@property
|
|
|
|
def _cache(self):
|
|
|
|
if getattr(self, '_client', None) is None:
|
|
|
|
self._client = self._lib.Client(self._servers, pickleProtocol=pickle.HIGHEST_PROTOCOL)
|
|
|
|
return self._client
|
|
|
|
|
2010-12-21 23:19:19 +08:00
|
|
|
class PyLibMCCache(BaseMemcachedCache):
|
|
|
|
"An implementation of a cache binding using pylibmc"
|
|
|
|
def __init__(self, server, params):
|
|
|
|
import pylibmc
|
|
|
|
self._local = local()
|
|
|
|
super(PyLibMCCache, self).__init__(server, params,
|
|
|
|
library=pylibmc,
|
|
|
|
value_not_found_exception=pylibmc.NotFound)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def _cache(self):
|
|
|
|
# PylibMC uses cache options as the 'behaviors' attribute.
|
|
|
|
# It also needs to use threadlocals, because some versions of
|
|
|
|
# PylibMC don't play well with the GIL.
|
|
|
|
client = getattr(self._local, 'client', None)
|
|
|
|
if client:
|
|
|
|
return client
|
|
|
|
|
|
|
|
client = self._lib.Client(self._servers)
|
|
|
|
if self._options:
|
|
|
|
client.behaviors = self._options
|
|
|
|
|
|
|
|
self._local.client = client
|
|
|
|
|
|
|
|
return client
|