Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

…ocks.
  • Loading branch information...
commit db2727f9524f46e0af1aed581a03ffeac4fbaafe 1 parent 126250a
Alex Gaynor authored July 08, 2013

Showing 1 changed file with 14 additions and 10 deletions. Show diff stats Hide diff stats

  1. 24  django/core/cache/backends/locmem.py
24  django/core/cache/backends/locmem.py
@@ -9,16 +9,17 @@
9 9
 from django.core.cache.backends.base import BaseCache, DEFAULT_TIMEOUT
10 10
 from django.utils.synch import RWLock
11 11
 
  12
+
12 13
 # Global in-memory store of cache data. Keyed by name, to provide
13 14
 # multiple named local memory caches.
14 15
 _caches = {}
15 16
 _expire_info = {}
16 17
 _locks = {}
17 18
 
  19
+
18 20
 class LocMemCache(BaseCache):
19 21
     def __init__(self, name, params):
20 22
         BaseCache.__init__(self, params)
21  
-        global _caches, _expire_info, _locks
22 23
         self._cache = _caches.setdefault(name, {})
23 24
         self._expire_info = _expire_info.setdefault(name, {})
24 25
         self._lock = _locks.setdefault(name, RWLock())
@@ -45,11 +46,11 @@ def get(self, key, default=None, version=None):
45 46
             exp = self._expire_info.get(key, 0)
46 47
             if exp is None or exp > time.time():
47 48
                 pickled = self._cache[key]
48  
-        try:
49  
-            if pickled is not None:
  49
+        if pickled is not None:
  50
+            try:
50 51
                 return pickle.loads(pickled)
51  
-        except pickle.PickleError:
52  
-            return default
  52
+            except pickle.PickleError:
  53
+                return default
53 54
 
54 55
         with self._lock.writer():
55 56
             try:
@@ -73,11 +74,12 @@ def set(self, key, value, timeout=DEFAULT_TIMEOUT, version=None):
73 74
         self.validate_key(key)
74 75
         try:
75 76
             pickled = pickle.dumps(value, pickle.HIGHEST_PROTOCOL)
76  
-            with self._lock.writer():
77  
-                self._set(key, pickled, timeout)
78 77
         except pickle.PickleError:
79 78
             pass
80  
-            
  79
+        else:
  80
+            with self._lock.writer():
  81
+                self._set(key, pickled, timeout)
  82
+
81 83
     def incr(self, key, delta=1, version=None):
82 84
         value = self.get(key, version=version)
83 85
         if value is None:
@@ -86,10 +88,11 @@ def incr(self, key, delta=1, version=None):
86 88
         key = self.make_key(key, version=version)
87 89
         try:
88 90
             pickled = pickle.dumps(new_value, pickle.HIGHEST_PROTOCOL)
89  
-            with self._lock.writer():
90  
-                self._cache[key] = pickled
91 91
         except pickle.PickleError:
92 92
             pass
  93
+        else:
  94
+            with self._lock.writer():
  95
+                self._cache[key] = pickled
93 96
         return new_value
94 97
 
95 98
     def has_key(self, key, version=None):
@@ -138,6 +141,7 @@ def clear(self):
138 141
         self._cache.clear()
139 142
         self._expire_info.clear()
140 143
 
  144
+
141 145
 # For backwards compatibility
142 146
 class CacheClass(LocMemCache):
143 147
     pass

0 notes on commit db2727f

Please sign in to comment.
Something went wrong with that request. Please try again.