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:
|
|
|
|
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)
|
2007-10-20 23:16:34 +08:00
|
|
|
self._lock.writer_enters()
|
|
|
|
try:
|
2007-12-11 09:20:25 +08:00
|
|
|
exp = self._expire_info.get(key)
|
|
|
|
if exp is None or exp <= time.time():
|
|
|
|
try:
|
|
|
|
self._set(key, pickle.dumps(value), 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
|
|
|
finally:
|
|
|
|
self._lock.writer_leaves()
|
|
|
|
|
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)
|
2006-02-24 14:07:01 +08:00
|
|
|
self._lock.reader_enters()
|
|
|
|
try:
|
|
|
|
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:
|
|
|
|
return pickle.loads(self._cache[key])
|
|
|
|
except pickle.PickleError:
|
|
|
|
return default
|
2006-02-24 14:07:01 +08:00
|
|
|
finally:
|
|
|
|
self._lock.reader_leaves()
|
2007-12-11 09:20:25 +08:00
|
|
|
self._lock.writer_enters()
|
|
|
|
try:
|
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
|
|
|
|
finally:
|
|
|
|
self._lock.writer_leaves()
|
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)
|
2006-02-24 14:07:01 +08:00
|
|
|
self._lock.writer_enters()
|
|
|
|
try:
|
2011-03-28 09:40:43 +08:00
|
|
|
self._set(key, pickle.dumps(value), timeout)
|
|
|
|
except pickle.PickleError:
|
|
|
|
pass
|
2006-02-24 14:07:01 +08:00
|
|
|
finally:
|
|
|
|
self._lock.writer_leaves()
|
|
|
|
|
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)
|
2007-12-11 09:20:25 +08:00
|
|
|
self._lock.reader_enters()
|
|
|
|
try:
|
|
|
|
exp = self._expire_info.get(key)
|
|
|
|
if exp is None:
|
|
|
|
return False
|
|
|
|
elif exp > time.time():
|
|
|
|
return True
|
|
|
|
finally:
|
|
|
|
self._lock.reader_leaves()
|
|
|
|
|
|
|
|
self._lock.writer_enters()
|
|
|
|
try:
|
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
|
|
|
|
finally:
|
|
|
|
self._lock.writer_leaves()
|
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)
|
2006-02-24 14:07:01 +08:00
|
|
|
self._lock.writer_enters()
|
|
|
|
try:
|
2007-12-02 08:35:14 +08:00
|
|
|
self._delete(key)
|
2006-02-24 14:07:01 +08:00
|
|
|
finally:
|
|
|
|
self._lock.writer_leaves()
|
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
|