2006-02-24 14:07:01 +08:00
|
|
|
"Thread-safe in-memory cache backend."
|
|
|
|
|
2007-07-15 14:24:54 +08:00
|
|
|
import time
|
|
|
|
try:
|
2012-07-20 22:16:57 +08:00
|
|
|
from django.utils.six.moves import cPickle as pickle
|
2007-07-15 14:24:54 +08:00
|
|
|
except ImportError:
|
|
|
|
import pickle
|
2006-02-24 14:07:01 +08:00
|
|
|
|
2013-05-18 18:54:59 +08:00
|
|
|
from django.core.cache.backends.base import BaseCache, DEFAULT_TIMEOUT
|
2007-07-15 14:29:45 +08:00
|
|
|
from django.utils.synch import RWLock
|
|
|
|
|
2013-07-08 07:26:52 +08:00
|
|
|
|
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 = {}
|
|
|
|
|
2013-07-08 07:26:52 +08:00
|
|
|
|
2010-12-21 23:19:19 +08:00
|
|
|
class LocMemCache(BaseCache):
|
|
|
|
def __init__(self, name, params):
|
|
|
|
BaseCache.__init__(self, params)
|
|
|
|
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
|
|
|
|
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)
|
2010-09-13 02:45:26 +08:00
|
|
|
self.validate_key(key)
|
2013-11-24 01:55:28 +08:00
|
|
|
pickled = pickle.dumps(value, pickle.HIGHEST_PROTOCOL)
|
2011-11-27 12:28:31 +08:00
|
|
|
with self._lock.writer():
|
2014-05-09 18:23:28 +08:00
|
|
|
if self._has_expired(key):
|
2013-06-22 20:56:55 +08:00
|
|
|
self._set(key, pickled, timeout)
|
|
|
|
return True
|
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)
|
2013-06-22 20:56:55 +08:00
|
|
|
pickled = None
|
2011-11-27 12:28:31 +08:00
|
|
|
with self._lock.reader():
|
2014-05-09 18:23:28 +08:00
|
|
|
if not self._has_expired(key):
|
2013-06-22 20:56:55 +08:00
|
|
|
pickled = self._cache[key]
|
2013-07-08 07:26:52 +08:00
|
|
|
if pickled is not None:
|
|
|
|
try:
|
2013-06-22 20:56:55 +08:00
|
|
|
return pickle.loads(pickled)
|
2013-07-08 07:26:52 +08:00
|
|
|
except pickle.PickleError:
|
|
|
|
return default
|
2013-06-22 20:56:55 +08:00
|
|
|
|
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
|
|
|
|
2013-05-18 18:54:59 +08:00
|
|
|
def _set(self, key, value, timeout=DEFAULT_TIMEOUT):
|
2007-12-02 08:35:14 +08:00
|
|
|
if len(self._cache) >= self._max_entries:
|
|
|
|
self._cull()
|
|
|
|
self._cache[key] = value
|
2013-09-24 00:40:19 +08:00
|
|
|
self._expire_info[key] = self.get_backend_timeout(timeout)
|
2007-12-02 08:35:14 +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)
|
2010-09-13 02:45:26 +08:00
|
|
|
self.validate_key(key)
|
2013-11-24 01:55:28 +08:00
|
|
|
pickled = pickle.dumps(value, pickle.HIGHEST_PROTOCOL)
|
|
|
|
with self._lock.writer():
|
|
|
|
self._set(key, pickled, timeout)
|
2013-07-08 07:26:52 +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)
|
2013-11-24 01:55:28 +08:00
|
|
|
pickled = pickle.dumps(new_value, pickle.HIGHEST_PROTOCOL)
|
|
|
|
with self._lock.writer():
|
|
|
|
self._cache[key] = pickled
|
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():
|
2014-05-09 18:23:28 +08:00
|
|
|
if not self._has_expired(key):
|
2007-12-11 09:20:25 +08:00
|
|
|
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
|
|
|
|
2014-05-09 18:23:28 +08:00
|
|
|
def _has_expired(self, key):
|
|
|
|
exp = self._expire_info.get(key, -1)
|
|
|
|
if exp is None or exp > time.time():
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
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()
|