Fixed 20613 - moved pickle loads/dumps outside with self._lock.reader()/writer() blocks
Additionaly added pickled=None initially and check for none before returning in get
This commit is contained in:
parent
1116df0751
commit
76ee39ce14
|
@ -26,28 +26,31 @@ class LocMemCache(BaseCache):
|
||||||
def add(self, key, value, timeout=DEFAULT_TIMEOUT, version=None):
|
def add(self, key, value, timeout=DEFAULT_TIMEOUT, version=None):
|
||||||
key = self.make_key(key, version=version)
|
key = self.make_key(key, version=version)
|
||||||
self.validate_key(key)
|
self.validate_key(key)
|
||||||
|
try:
|
||||||
|
pickled = pickle.dumps(value, pickle.HIGHEST_PROTOCOL)
|
||||||
|
except pickle.PickleError:
|
||||||
|
return False
|
||||||
with self._lock.writer():
|
with self._lock.writer():
|
||||||
exp = self._expire_info.get(key)
|
exp = self._expire_info.get(key)
|
||||||
if exp is None or exp <= time.time():
|
if exp is None or exp <= time.time():
|
||||||
try:
|
|
||||||
pickled = pickle.dumps(value, pickle.HIGHEST_PROTOCOL)
|
|
||||||
self._set(key, pickled, timeout)
|
self._set(key, pickled, timeout)
|
||||||
return True
|
return True
|
||||||
except pickle.PickleError:
|
|
||||||
pass
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def get(self, key, default=None, version=None):
|
def get(self, key, default=None, version=None):
|
||||||
key = self.make_key(key, version=version)
|
key = self.make_key(key, version=version)
|
||||||
self.validate_key(key)
|
self.validate_key(key)
|
||||||
|
pickled = None
|
||||||
with self._lock.reader():
|
with self._lock.reader():
|
||||||
exp = self._expire_info.get(key, 0)
|
exp = self._expire_info.get(key, 0)
|
||||||
if exp is None or exp > time.time():
|
if exp is None or exp > time.time():
|
||||||
try:
|
|
||||||
pickled = self._cache[key]
|
pickled = self._cache[key]
|
||||||
|
try:
|
||||||
|
if pickled is not None:
|
||||||
return pickle.loads(pickled)
|
return pickle.loads(pickled)
|
||||||
except pickle.PickleError:
|
except pickle.PickleError:
|
||||||
return default
|
return default
|
||||||
|
|
||||||
with self._lock.writer():
|
with self._lock.writer():
|
||||||
try:
|
try:
|
||||||
del self._cache[key]
|
del self._cache[key]
|
||||||
|
@ -68,9 +71,9 @@ class LocMemCache(BaseCache):
|
||||||
def set(self, key, value, timeout=DEFAULT_TIMEOUT, version=None):
|
def set(self, key, value, timeout=DEFAULT_TIMEOUT, version=None):
|
||||||
key = self.make_key(key, version=version)
|
key = self.make_key(key, version=version)
|
||||||
self.validate_key(key)
|
self.validate_key(key)
|
||||||
with self._lock.writer():
|
|
||||||
try:
|
try:
|
||||||
pickled = pickle.dumps(value, pickle.HIGHEST_PROTOCOL)
|
pickled = pickle.dumps(value, pickle.HIGHEST_PROTOCOL)
|
||||||
|
with self._lock.writer():
|
||||||
self._set(key, pickled, timeout)
|
self._set(key, pickled, timeout)
|
||||||
except pickle.PickleError:
|
except pickle.PickleError:
|
||||||
pass
|
pass
|
||||||
|
@ -81,9 +84,9 @@ class LocMemCache(BaseCache):
|
||||||
raise ValueError("Key '%s' not found" % key)
|
raise ValueError("Key '%s' not found" % key)
|
||||||
new_value = value + delta
|
new_value = value + delta
|
||||||
key = self.make_key(key, version=version)
|
key = self.make_key(key, version=version)
|
||||||
with self._lock.writer():
|
|
||||||
try:
|
try:
|
||||||
pickled = pickle.dumps(new_value, pickle.HIGHEST_PROTOCOL)
|
pickled = pickle.dumps(new_value, pickle.HIGHEST_PROTOCOL)
|
||||||
|
with self._lock.writer():
|
||||||
self._cache[key] = pickled
|
self._cache[key] = pickled
|
||||||
except pickle.PickleError:
|
except pickle.PickleError:
|
||||||
pass
|
pass
|
||||||
|
|
Loading…
Reference in New Issue