2006-02-24 14:07:01 +08:00
|
|
|
"Thread-safe in-memory cache backend."
|
|
|
|
|
2011-11-27 12:28:31 +08:00
|
|
|
from __future__ import with_statement
|
|
|
|
|
2007-07-15 14:24:54 +08:00
|
|
|
import time
|
|
|
|
try:
|
|
|
|
import cPickle as pickle
|
|
|
|
except ImportError:
|
|
|
|
import pickle
|
2006-02-24 14:07:01 +08:00
|
|
|
|
2007-12-02 08:35:14 +08:00
|
|
|
from django.core.cache.backends.base import BaseCache
|
2007-07-15 14:29:45 +08:00
|
|
|
from django.utils.synch import RWLock
|
|
|
|
|
2010-12-21 23:19:19 +08:00
|
|
|
# Global in-memory store of cache data. Keyed by name, to provide
|
|
|
|
# multiple named local memory caches.
|
|
|
|
_caches = {}
|
|
|
|
_expire_info = {}
|
|
|
|
_locks = {}
|
|
|
|
|
|
|
|
class LocMemCache(BaseCache):
|
|
|
|
def __init__(self, name, params):
|
|
|
|
BaseCache.__init__(self, params)
|
|
|
|
global _caches, _expire_info, _locks
|
|
|
|
self._cache = _caches.setdefault(name, {})
|
|
|
|
self._expire_info = _expire_info.setdefault(name, {})
|
|
|
|
self._lock = _locks.setdefault(name, RWLock())
|
2006-02-24 14:07:01 +08:00
|
|
|
|
2010-11-19 23:39:35 +08:00
|
|
|
def add(self, key, value, timeout=None, version=None):
|
|
|
|
key = self.make_key(key, version=version)
|
2010-09-13 02:45:26 +08:00
|
|
|
self.validate_key(key)
|
2011-11-27 12:28:31 +08:00
|
|
|
with self._lock.writer():
|
2007-12-11 09:20:25 +08:00
|
|
|
exp = self._expire_info.get(key)
|
|
|
|
if exp is None or exp <= time.time():
|
|
|
|
try:
|
2011-11-22 06:25:49 +08:00
|
|
|
pickled = pickle.dumps(value, pickle.HIGHEST_PROTOCOL)
|
|
|
|
self._set(key, pickled, timeout)
|
2008-08-10 11:52:21 +08:00
|
|
|
return True
|
2007-12-11 09:20:25 +08:00
|
|
|
except pickle.PickleError:
|
|
|
|
pass
|
2008-08-10 11:52:21 +08:00
|
|
|
return False
|
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)
|
2010-09-13 02:45:26 +08:00
|
|
|
self.validate_key(key)
|
2011-11-27 12:28:31 +08:00
|
|
|
with self._lock.reader():
|
2006-02-24 14:07:01 +08:00
|
|
|
exp = self._expire_info.get(key)
|
|
|
|
if exp is None:
|
|
|
|
return default
|
2007-12-11 09:20:25 +08:00
|
|
|
elif exp > time.time():
|
2007-07-15 14:24:54 +08:00
|
|
|
try:
|
2011-11-22 06:25:49 +08:00
|
|
|
pickled = self._cache[key]
|
|
|
|
return pickle.loads(pickled)
|
2007-07-15 14:24:54 +08:00
|
|
|
except pickle.PickleError:
|
|
|
|
return default
|
2011-11-27 12:28:31 +08:00
|
|
|
with self._lock.writer():
|
2009-04-02 01:05:19 +08:00
|
|
|
try:
|
|
|
|
del self._cache[key]
|
|
|
|
del self._expire_info[key]
|
|
|
|
except KeyError:
|
|
|
|
pass
|
2007-12-11 09:20:25 +08:00
|
|
|
return default
|
2006-02-24 14:07:01 +08:00
|
|
|
|
2007-12-02 08:35:14 +08:00
|
|
|
def _set(self, key, value, timeout=None):
|
|
|
|
if len(self._cache) >= self._max_entries:
|
|
|
|
self._cull()
|
|
|
|
if timeout is None:
|
|
|
|
timeout = self.default_timeout
|
|
|
|
self._cache[key] = value
|
|
|
|
self._expire_info[key] = time.time() + timeout
|
|
|
|
|
2010-11-19 23:39:35 +08:00
|
|
|
def set(self, key, value, timeout=None, version=None):
|
|
|
|
key = self.make_key(key, version=version)
|
2010-09-13 02:45:26 +08:00
|
|
|
self.validate_key(key)
|
2011-11-27 12:28:31 +08:00
|
|
|
with self._lock.writer():
|
|
|
|
try:
|
|
|
|
pickled = pickle.dumps(value, pickle.HIGHEST_PROTOCOL)
|
|
|
|
self._set(key, pickled, timeout)
|
|
|
|
except pickle.PickleError:
|
|
|
|
pass
|
2006-02-24 14:07:01 +08:00
|
|
|
|
2011-11-27 06:27:16 +08:00
|
|
|
def incr(self, key, delta=1, version=None):
|
|
|
|
value = self.get(key, version=version)
|
|
|
|
if value is None:
|
|
|
|
raise ValueError("Key '%s' not found" % key)
|
|
|
|
new_value = value + delta
|
|
|
|
key = self.make_key(key, version=version)
|
2011-11-27 12:28:31 +08:00
|
|
|
with self._lock.writer():
|
|
|
|
try:
|
|
|
|
pickled = pickle.dumps(new_value, pickle.HIGHEST_PROTOCOL)
|
|
|
|
self._cache[key] = pickled
|
|
|
|
except pickle.PickleError:
|
|
|
|
pass
|
2011-11-27 06:27:16 +08:00
|
|
|
return new_value
|
|
|
|
|
2010-11-19 23:39:35 +08:00
|
|
|
def has_key(self, key, version=None):
|
|
|
|
key = self.make_key(key, version=version)
|
2010-09-13 02:45:26 +08:00
|
|
|
self.validate_key(key)
|
2011-11-27 12:28:31 +08:00
|
|
|
with self._lock.reader():
|
2007-12-11 09:20:25 +08:00
|
|
|
exp = self._expire_info.get(key)
|
|
|
|
if exp is None:
|
|
|
|
return False
|
|
|
|
elif exp > time.time():
|
|
|
|
return True
|
|
|
|
|
2011-11-27 12:28:31 +08:00
|
|
|
with self._lock.writer():
|
2009-03-08 17:59:43 +08:00
|
|
|
try:
|
|
|
|
del self._cache[key]
|
|
|
|
del self._expire_info[key]
|
|
|
|
except KeyError:
|
|
|
|
pass
|
2007-12-11 09:20:25 +08:00
|
|
|
return False
|
2007-12-02 08:35:14 +08:00
|
|
|
|
|
|
|
def _cull(self):
|
|
|
|
if self._cull_frequency == 0:
|
2010-01-27 16:21:35 +08:00
|
|
|
self.clear()
|
2007-12-02 08:35:14 +08:00
|
|
|
else:
|
|
|
|
doomed = [k for (i, k) in enumerate(self._cache) if i % self._cull_frequency == 0]
|
|
|
|
for k in doomed:
|
2008-07-26 13:27:59 +08:00
|
|
|
self._delete(k)
|
2007-12-02 08:35:14 +08:00
|
|
|
|
|
|
|
def _delete(self, key):
|
|
|
|
try:
|
|
|
|
del self._cache[key]
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
try:
|
|
|
|
del self._expire_info[key]
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
|
2010-11-19 23:39:35 +08:00
|
|
|
def delete(self, key, version=None):
|
|
|
|
key = self.make_key(key, version=version)
|
2010-09-13 02:45:26 +08:00
|
|
|
self.validate_key(key)
|
2011-11-27 12:28:31 +08:00
|
|
|
with self._lock.writer():
|
2007-12-02 08:35:14 +08:00
|
|
|
self._delete(key)
|
2010-01-27 16:21:35 +08:00
|
|
|
|
|
|
|
def clear(self):
|
|
|
|
self._cache.clear()
|
|
|
|
self._expire_info.clear()
|
2010-12-21 23:19:19 +08:00
|
|
|
|
|
|
|
# For backwards compatibility
|
|
|
|
class CacheClass(LocMemCache):
|
|
|
|
pass
|