Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge branch 'ticket_20613' of https://github.com/ersran9/django into…

… t20613
  • Loading branch information...
commit 126250a4c422f66469eb70d792dcfd65b1ac5178 2 parents d66bd1d + 76ee39c
Alex Gaynor authored

Showing 1 changed file with 25 additions and 22 deletions. Show diff stats Hide diff stats

  1. 47  django/core/cache/backends/locmem.py
47  django/core/cache/backends/locmem.py
@@ -26,28 +26,31 @@ def __init__(self, name, params):
26 26
     def add(self, key, value, timeout=DEFAULT_TIMEOUT, version=None):
27 27
         key = self.make_key(key, version=version)
28 28
         self.validate_key(key)
  29
+        try:
  30
+            pickled = pickle.dumps(value, pickle.HIGHEST_PROTOCOL)
  31
+        except pickle.PickleError:
  32
+            return False
29 33
         with self._lock.writer():
30 34
             exp = self._expire_info.get(key)
31 35
             if exp is None or exp <= time.time():
32  
-                try:
33  
-                    pickled = pickle.dumps(value, pickle.HIGHEST_PROTOCOL)
34  
-                    self._set(key, pickled, timeout)
35  
-                    return True
36  
-                except pickle.PickleError:
37  
-                    pass
  36
+                self._set(key, pickled, timeout)
  37
+                return True
38 38
             return False
39 39
 
40 40
     def get(self, key, default=None, version=None):
41 41
         key = self.make_key(key, version=version)
42 42
         self.validate_key(key)
  43
+        pickled = None
43 44
         with self._lock.reader():
44 45
             exp = self._expire_info.get(key, 0)
45 46
             if exp is None or exp > time.time():
46  
-                try:
47  
-                    pickled = self._cache[key]
48  
-                    return pickle.loads(pickled)
49  
-                except pickle.PickleError:
50  
-                    return default
  47
+                pickled = self._cache[key]
  48
+        try:
  49
+            if pickled is not None:
  50
+                return pickle.loads(pickled)
  51
+        except pickle.PickleError:
  52
+            return default
  53
+
51 54
         with self._lock.writer():
52 55
             try:
53 56
                 del self._cache[key]
@@ -68,25 +71,25 @@ def _set(self, key, value, timeout=DEFAULT_TIMEOUT):
68 71
     def set(self, key, value, timeout=DEFAULT_TIMEOUT, version=None):
69 72
         key = self.make_key(key, version=version)
70 73
         self.validate_key(key)
71  
-        with self._lock.writer():
72  
-            try:
73  
-                pickled = pickle.dumps(value, pickle.HIGHEST_PROTOCOL)
  74
+        try:
  75
+            pickled = pickle.dumps(value, pickle.HIGHEST_PROTOCOL)
  76
+            with self._lock.writer():
74 77
                 self._set(key, pickled, timeout)
75  
-            except pickle.PickleError:
76  
-                pass
77  
-
  78
+        except pickle.PickleError:
  79
+            pass
  80
+            
78 81
     def incr(self, key, delta=1, version=None):
79 82
         value = self.get(key, version=version)
80 83
         if value is None:
81 84
             raise ValueError("Key '%s' not found" % key)
82 85
         new_value = value + delta
83 86
         key = self.make_key(key, version=version)
84  
-        with self._lock.writer():
85  
-            try:
86  
-                pickled = pickle.dumps(new_value, pickle.HIGHEST_PROTOCOL)
  87
+        try:
  88
+            pickled = pickle.dumps(new_value, pickle.HIGHEST_PROTOCOL)
  89
+            with self._lock.writer():
87 90
                 self._cache[key] = pickled
88  
-            except pickle.PickleError:
89  
-                pass
  91
+        except pickle.PickleError:
  92
+            pass
90 93
         return new_value
91 94
 
92 95
     def has_key(self, key, version=None):

0 notes on commit 126250a

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