2006-02-24 14:07:01 +08:00
|
|
|
"Thread-safe in-memory cache backend."
|
2017-01-07 19:11:46 +08:00
|
|
|
import pickle
|
2007-07-15 14:24:54 +08:00
|
|
|
import time
|
2018-01-25 01:26:19 +08:00
|
|
|
from collections import OrderedDict
|
|
|
|
from threading import Lock
|
2015-01-28 20:35:27 +08:00
|
|
|
|
|
|
|
from django.core.cache.backends.base import DEFAULT_TIMEOUT, BaseCache
|
|
|
|
|
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):
|
2018-07-07 05:55:46 +08:00
|
|
|
pickle_protocol = pickle.HIGHEST_PROTOCOL
|
|
|
|
|
2010-12-21 23:19:19 +08:00
|
|
|
def __init__(self, name, params):
|
2017-12-07 22:10:32 +08:00
|
|
|
super().__init__(params)
|
2018-01-25 01:26:19 +08:00
|
|
|
self._cache = _caches.setdefault(name, OrderedDict())
|
2010-12-21 23:19:19 +08:00
|
|
|
self._expire_info = _expire_info.setdefault(name, {})
|
2018-01-25 01:26:19 +08:00
|
|
|
self._lock = _locks.setdefault(name, Lock())
|
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)
|
2018-07-07 05:55:46 +08:00
|
|
|
pickled = pickle.dumps(value, self.pickle_protocol)
|
2018-01-25 01:26:19 +08:00
|
|
|
with self._lock:
|
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
|
|
|
|
2018-01-25 01:26:19 +08:00
|
|
|
def get(self, key, default=None, 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)
|
2018-01-25 01:26:19 +08:00
|
|
|
with self._lock:
|
|
|
|
if self._has_expired(key):
|
|
|
|
self._delete(key)
|
2013-07-08 07:26:52 +08:00
|
|
|
return default
|
2018-01-25 01:26:19 +08:00
|
|
|
pickled = self._cache[key]
|
|
|
|
self._cache.move_to_end(key, last=False)
|
|
|
|
return pickle.loads(pickled)
|
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
|
2018-01-25 01:26:19 +08:00
|
|
|
self._cache.move_to_end(key, last=False)
|
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)
|
2018-07-07 05:55:46 +08:00
|
|
|
pickled = pickle.dumps(value, self.pickle_protocol)
|
2018-01-25 01:26:19 +08:00
|
|
|
with self._lock:
|
2013-11-24 01:55:28 +08:00
|
|
|
self._set(key, pickled, timeout)
|
2013-07-08 07:26:52 +08:00
|
|
|
|
2018-04-28 05:48:35 +08:00
|
|
|
def touch(self, key, timeout=DEFAULT_TIMEOUT, version=None):
|
|
|
|
key = self.make_key(key, version=version)
|
|
|
|
with self._lock:
|
|
|
|
if self._has_expired(key):
|
|
|
|
return False
|
|
|
|
self._expire_info[key] = self.get_backend_timeout(timeout)
|
|
|
|
return True
|
|
|
|
|
2011-11-27 06:27:16 +08:00
|
|
|
def incr(self, key, delta=1, version=None):
|
2018-01-25 01:26:19 +08:00
|
|
|
key = self.make_key(key, version=version)
|
|
|
|
self.validate_key(key)
|
|
|
|
with self._lock:
|
|
|
|
if self._has_expired(key):
|
|
|
|
self._delete(key)
|
2014-10-12 21:37:17 +08:00
|
|
|
raise ValueError("Key '%s' not found" % key)
|
2018-01-25 01:26:19 +08:00
|
|
|
pickled = self._cache[key]
|
|
|
|
value = pickle.loads(pickled)
|
2014-10-12 21:37:17 +08:00
|
|
|
new_value = value + delta
|
2018-07-07 05:55:46 +08:00
|
|
|
pickled = pickle.dumps(new_value, self.pickle_protocol)
|
2013-11-24 01:55:28 +08:00
|
|
|
self._cache[key] = pickled
|
2018-01-25 01:26:19 +08:00
|
|
|
self._cache.move_to_end(key, last=False)
|
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)
|
2018-01-25 01:26:19 +08:00
|
|
|
with self._lock:
|
|
|
|
if self._has_expired(key):
|
|
|
|
self._delete(key)
|
|
|
|
return False
|
|
|
|
return True
|
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)
|
2018-01-04 07:52:12 +08:00
|
|
|
return exp is not None and exp <= time.time()
|
2014-05-09 18:23:28 +08:00
|
|
|
|
2007-12-02 08:35:14 +08:00
|
|
|
def _cull(self):
|
|
|
|
if self._cull_frequency == 0:
|
2018-01-25 01:26:19 +08:00
|
|
|
self._cache.clear()
|
|
|
|
self._expire_info.clear()
|
2007-12-02 08:35:14 +08:00
|
|
|
else:
|
2018-01-25 01:26:19 +08:00
|
|
|
count = len(self._cache) // self._cull_frequency
|
|
|
|
for i in range(count):
|
|
|
|
key, _ = self._cache.popitem()
|
|
|
|
del self._expire_info[key]
|
2007-12-02 08:35:14 +08:00
|
|
|
|
|
|
|
def _delete(self, key):
|
2017-09-07 20:16:21 +08:00
|
|
|
try:
|
2007-12-02 08:35:14 +08:00
|
|
|
del self._cache[key]
|
|
|
|
del self._expire_info[key]
|
2017-09-07 20:16:21 +08:00
|
|
|
except KeyError:
|
2019-10-08 17:02:40 +08:00
|
|
|
return False
|
|
|
|
return True
|
2007-12-02 08:35:14 +08:00
|
|
|
|
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)
|
2018-01-25 01:26:19 +08:00
|
|
|
with self._lock:
|
2019-10-08 17:02:40 +08:00
|
|
|
return self._delete(key)
|
2010-01-27 16:21:35 +08:00
|
|
|
|
|
|
|
def clear(self):
|
2018-01-25 01:26:19 +08:00
|
|
|
with self._lock:
|
|
|
|
self._cache.clear()
|
|
|
|
self._expire_info.clear()
|