mirror of
https://github.com/django/django.git
synced 2024-12-25 10:35:48 +00:00
A few cleanups to capture the minimum amount of code in try/except blocks.
This commit is contained in:
parent
126250a4c4
commit
db2727f952
24
django/core/cache/backends/locmem.py
vendored
24
django/core/cache/backends/locmem.py
vendored
@ -9,16 +9,17 @@ except ImportError:
|
|||||||
from django.core.cache.backends.base import BaseCache, DEFAULT_TIMEOUT
|
from django.core.cache.backends.base import BaseCache, DEFAULT_TIMEOUT
|
||||||
from django.utils.synch import RWLock
|
from django.utils.synch import RWLock
|
||||||
|
|
||||||
|
|
||||||
# Global in-memory store of cache data. Keyed by name, to provide
|
# Global in-memory store of cache data. Keyed by name, to provide
|
||||||
# multiple named local memory caches.
|
# multiple named local memory caches.
|
||||||
_caches = {}
|
_caches = {}
|
||||||
_expire_info = {}
|
_expire_info = {}
|
||||||
_locks = {}
|
_locks = {}
|
||||||
|
|
||||||
|
|
||||||
class LocMemCache(BaseCache):
|
class LocMemCache(BaseCache):
|
||||||
def __init__(self, name, params):
|
def __init__(self, name, params):
|
||||||
BaseCache.__init__(self, params)
|
BaseCache.__init__(self, params)
|
||||||
global _caches, _expire_info, _locks
|
|
||||||
self._cache = _caches.setdefault(name, {})
|
self._cache = _caches.setdefault(name, {})
|
||||||
self._expire_info = _expire_info.setdefault(name, {})
|
self._expire_info = _expire_info.setdefault(name, {})
|
||||||
self._lock = _locks.setdefault(name, RWLock())
|
self._lock = _locks.setdefault(name, RWLock())
|
||||||
@ -45,11 +46,11 @@ class LocMemCache(BaseCache):
|
|||||||
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():
|
||||||
pickled = self._cache[key]
|
pickled = self._cache[key]
|
||||||
try:
|
if pickled is not None:
|
||||||
if pickled is not None:
|
try:
|
||||||
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:
|
||||||
@ -73,11 +74,12 @@ class LocMemCache(BaseCache):
|
|||||||
self.validate_key(key)
|
self.validate_key(key)
|
||||||
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)
|
|
||||||
except pickle.PickleError:
|
except pickle.PickleError:
|
||||||
pass
|
pass
|
||||||
|
else:
|
||||||
|
with self._lock.writer():
|
||||||
|
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)
|
value = self.get(key, version=version)
|
||||||
if value is None:
|
if value is None:
|
||||||
@ -86,10 +88,11 @@ class LocMemCache(BaseCache):
|
|||||||
key = self.make_key(key, version=version)
|
key = self.make_key(key, version=version)
|
||||||
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
|
|
||||||
except pickle.PickleError:
|
except pickle.PickleError:
|
||||||
pass
|
pass
|
||||||
|
else:
|
||||||
|
with self._lock.writer():
|
||||||
|
self._cache[key] = pickled
|
||||||
return new_value
|
return new_value
|
||||||
|
|
||||||
def has_key(self, key, version=None):
|
def has_key(self, key, version=None):
|
||||||
@ -138,6 +141,7 @@ class LocMemCache(BaseCache):
|
|||||||
self._cache.clear()
|
self._cache.clear()
|
||||||
self._expire_info.clear()
|
self._expire_info.clear()
|
||||||
|
|
||||||
|
|
||||||
# For backwards compatibility
|
# For backwards compatibility
|
||||||
class CacheClass(LocMemCache):
|
class CacheClass(LocMemCache):
|
||||||
pass
|
pass
|
||||||
|
Loading…
Reference in New Issue
Block a user