Fixed #23642 -- Made LocMemCache.incr() thread-safe as documented
This commit is contained in:
parent
c48a29a02a
commit
6448dd8335
|
@ -1,5 +1,6 @@
|
||||||
"Thread-safe in-memory cache backend."
|
"Thread-safe in-memory cache backend."
|
||||||
|
|
||||||
|
from contextlib import contextmanager
|
||||||
import time
|
import time
|
||||||
try:
|
try:
|
||||||
from django.utils.six.moves import cPickle as pickle
|
from django.utils.six.moves import cPickle as pickle
|
||||||
|
@ -17,6 +18,12 @@ _expire_info = {}
|
||||||
_locks = {}
|
_locks = {}
|
||||||
|
|
||||||
|
|
||||||
|
@contextmanager
|
||||||
|
def dummy():
|
||||||
|
"""A context manager that does nothing special."""
|
||||||
|
yield
|
||||||
|
|
||||||
|
|
||||||
class LocMemCache(BaseCache):
|
class LocMemCache(BaseCache):
|
||||||
def __init__(self, name, params):
|
def __init__(self, name, params):
|
||||||
BaseCache.__init__(self, params)
|
BaseCache.__init__(self, params)
|
||||||
|
@ -34,11 +41,11 @@ class LocMemCache(BaseCache):
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def get(self, key, default=None, version=None):
|
def get(self, key, default=None, version=None, acquire_lock=True):
|
||||||
key = self.make_key(key, version=version)
|
key = self.make_key(key, version=version)
|
||||||
self.validate_key(key)
|
self.validate_key(key)
|
||||||
pickled = None
|
pickled = None
|
||||||
with self._lock.reader():
|
with (self._lock.reader() if acquire_lock else dummy()):
|
||||||
if not self._has_expired(key):
|
if not self._has_expired(key):
|
||||||
pickled = self._cache[key]
|
pickled = self._cache[key]
|
||||||
if pickled is not None:
|
if pickled is not None:
|
||||||
|
@ -47,7 +54,7 @@ class LocMemCache(BaseCache):
|
||||||
except pickle.PickleError:
|
except pickle.PickleError:
|
||||||
return default
|
return default
|
||||||
|
|
||||||
with self._lock.writer():
|
with (self._lock.writer() if acquire_lock else dummy()):
|
||||||
try:
|
try:
|
||||||
del self._cache[key]
|
del self._cache[key]
|
||||||
del self._expire_info[key]
|
del self._expire_info[key]
|
||||||
|
@ -69,13 +76,13 @@ class LocMemCache(BaseCache):
|
||||||
self._set(key, pickled, timeout)
|
self._set(key, pickled, timeout)
|
||||||
|
|
||||||
def incr(self, key, delta=1, version=None):
|
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)
|
|
||||||
pickled = pickle.dumps(new_value, pickle.HIGHEST_PROTOCOL)
|
|
||||||
with self._lock.writer():
|
with self._lock.writer():
|
||||||
|
value = self.get(key, version=version, acquire_lock=False)
|
||||||
|
if value is None:
|
||||||
|
raise ValueError("Key '%s' not found" % key)
|
||||||
|
new_value = value + delta
|
||||||
|
key = self.make_key(key, version=version)
|
||||||
|
pickled = pickle.dumps(new_value, pickle.HIGHEST_PROTOCOL)
|
||||||
self._cache[key] = pickled
|
self._cache[key] = pickled
|
||||||
return new_value
|
return new_value
|
||||||
|
|
||||||
|
|
|
@ -157,7 +157,8 @@ Minor features
|
||||||
Cache
|
Cache
|
||||||
^^^^^
|
^^^^^
|
||||||
|
|
||||||
* ...
|
* The ``incr()`` method of the
|
||||||
|
``django.core.cache.backends.locmem.LocMemCache`` backend is now thread-safe.
|
||||||
|
|
||||||
Email
|
Email
|
||||||
^^^^^
|
^^^^^
|
||||||
|
|
Loading…
Reference in New Issue