1
0
mirror of https://github.com/django/django.git synced 2024-12-25 02:26:12 +00:00

A few cleanups to capture the minimum amount of code in try/except blocks.

This commit is contained in:
Alex Gaynor 2013-07-08 09:26:52 +10:00
parent 126250a4c4
commit db2727f952

View File

@ -9,16 +9,17 @@ except ImportError:
from django.core.cache.backends.base import BaseCache, DEFAULT_TIMEOUT
from django.utils.synch import RWLock
# 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())
@ -45,11 +46,11 @@ class LocMemCache(BaseCache):
exp = self._expire_info.get(key, 0)
if exp is None or exp > time.time():
pickled = self._cache[key]
try:
if pickled is not None:
if pickled is not None:
try:
return pickle.loads(pickled)
except pickle.PickleError:
return default
except pickle.PickleError:
return default
with self._lock.writer():
try:
@ -73,11 +74,12 @@ class LocMemCache(BaseCache):
self.validate_key(key)
try:
pickled = pickle.dumps(value, pickle.HIGHEST_PROTOCOL)
with self._lock.writer():
self._set(key, pickled, timeout)
except pickle.PickleError:
pass
else:
with self._lock.writer():
self._set(key, pickled, timeout)
def incr(self, key, delta=1, version=None):
value = self.get(key, version=version)
if value is None:
@ -86,10 +88,11 @@ class LocMemCache(BaseCache):
key = self.make_key(key, version=version)
try:
pickled = pickle.dumps(new_value, pickle.HIGHEST_PROTOCOL)
with self._lock.writer():
self._cache[key] = pickled
except pickle.PickleError:
pass
else:
with self._lock.writer():
self._cache[key] = pickled
return new_value
def has_key(self, key, version=None):
@ -138,6 +141,7 @@ class LocMemCache(BaseCache):
self._cache.clear()
self._expire_info.clear()
# For backwards compatibility
class CacheClass(LocMemCache):
pass