Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

A few cleanups to capture the minimum amount of code in try/except bl…

…ocks.
  • Loading branch information...
commit db2727f9524f46e0af1aed581a03ffeac4fbaafe 1 parent 126250a
@alex alex authored
Showing with 14 additions and 10 deletions.
  1. +14 −10 django/core/cache/backends/locmem.py
View
24 django/core/cache/backends/locmem.py
@@ -9,16 +9,17 @@
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 @@ def get(self, key, default=None, version=None):
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 @@ def set(self, key, value, timeout=DEFAULT_TIMEOUT, version=None):
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 @@ def incr(self, key, delta=1, version=None):
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 @@ def clear(self):
self._cache.clear()
self._expire_info.clear()
+
# For backwards compatibility
class CacheClass(LocMemCache):
pass
Please sign in to comment.
Something went wrong with that request. Please try again.