2006-02-24 14:07:01 +08:00
|
|
|
"Memcached cache backend"
|
|
|
|
|
|
|
|
from django.core.cache.backends.base import BaseCache, InvalidCacheBackendError
|
|
|
|
|
|
|
|
try:
|
2007-03-26 07:29:31 +08:00
|
|
|
import cmemcache as memcache
|
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(';'))
|
|
|
|
|
|
|
|
def get(self, key, default=None):
|
|
|
|
val = self._cache.get(key)
|
|
|
|
if val is None:
|
|
|
|
return default
|
|
|
|
else:
|
|
|
|
return val
|
|
|
|
|
|
|
|
def set(self, key, value, timeout=0):
|
2006-08-18 23:10:08 +08:00
|
|
|
self._cache.set(key, value, timeout or self.default_timeout)
|
2006-02-24 14:07:01 +08:00
|
|
|
|
|
|
|
def delete(self, key):
|
|
|
|
self._cache.delete(key)
|
|
|
|
|
|
|
|
def get_many(self, keys):
|
|
|
|
return self._cache.get_multi(keys)
|