Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #3012 -- Changed the locmem cache backend to use pickle instead…

… of deepcopy to make it compatible with iterators (which cannot be copied). Patch from Sundance.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@5703 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit ae7f04caab1b4f2a2b509b036499e4e042caaac6 1 parent 208352e
Gary Wilson Jr. authored July 15, 2007
1  AUTHORS
@@ -237,6 +237,7 @@ answer newbie questions, and generally made Django that much better:
237 237
     Vasiliy Stavenko <stavenko@gmail.com>
238 238
     Thomas Steinacher <http://www.eggdrop.ch/>
239 239
     nowell strite
  240
+    Sundance
240 241
     Radek Švarz <http://www.svarz.cz/translate/>
241 242
     Swaroop C H <http://www.swaroopch.info>
242 243
     Aaron Swartz <http://www.aaronsw.com/>
16  django/core/cache/backends/locmem.py
@@ -2,7 +2,11 @@
2 2
 
3 3
 from django.core.cache.backends.simple import CacheClass as SimpleCacheClass
4 4
 from django.utils.synch import RWLock
5  
-import copy, time
  5
+import time
  6
+try:
  7
+    import cPickle as pickle
  8
+except ImportError:
  9
+    import pickle
6 10
 
7 11
 class CacheClass(SimpleCacheClass):
8 12
     def __init__(self, host, params):
@@ -20,7 +24,10 @@ def get(self, key, default=None):
20 24
             elif exp < now:
21 25
                 should_delete = True
22 26
             else:
23  
-                return copy.deepcopy(self._cache[key])
  27
+                try:
  28
+                    return pickle.loads(self._cache[key])
  29
+                except pickle.PickleError:
  30
+                    return default
24 31
         finally:
25 32
             self._lock.reader_leaves()
26 33
         if should_delete:
@@ -35,7 +42,10 @@ def get(self, key, default=None):
35 42
     def set(self, key, value, timeout=None):
36 43
         self._lock.writer_enters()
37 44
         try:
38  
-            SimpleCacheClass.set(self, key, value, timeout)
  45
+            try:
  46
+                super(CacheClass, self).set(key, pickle.dumps(value), timeout)
  47
+            except pickle.PickleError:
  48
+                pass
39 49
         finally:
40 50
             self._lock.writer_leaves()
41 51
 
14  tests/regressiontests/cache/tests.py
@@ -4,7 +4,7 @@
4 4
 from django.core.cache import cache
5 5
 import time, unittest
6 6
 
7  
-# functions/classes for complex data type tests        
  7
+# functions/classes for complex data type tests
8 8
 def f():
9 9
     return 42
10 10
 class C:
@@ -46,13 +46,12 @@ def test_has_key(self):
46 46
         self.assertEqual(cache.has_key("hello"), True)
47 47
         self.assertEqual(cache.has_key("goodbye"), False)
48 48
 
49  
-    def test_in(self): 
50  
-        cache.set("hello", "goodbye") 
51  
-        self.assertEqual("hello" in cache, True) 
52  
-        self.assertEqual("goodbye" in cache, False) 
  49
+    def test_in(self):
  50
+        cache.set("hello", "goodbye")
  51
+        self.assertEqual("hello" in cache, True)
  52
+        self.assertEqual("goodbye" in cache, False)
53 53
 
54 54
     def test_data_types(self):
55  
-        # test data types
56 55
         stuff = {
57 56
             'string'    : 'this is a string',
58 57
             'int'       : 42,
@@ -61,11 +60,12 @@ def test_data_types(self):
61 60
             'dict'      : {'A': 1, 'B' : 2},
62 61
             'function'  : f,
63 62
             'class'     : C,
  63
+            'iter'      : iter([1, 2 ,3]),
64 64
         }
65 65
         for (key, value) in stuff.items():
66 66
             cache.set(key, value)
67 67
             self.assertEqual(cache.get(key), value)
68  
-    
  68
+
69 69
     def test_expiration(self):
70 70
         # expiration
71 71
         cache.set('expire', 'very quickly', 1)

0 notes on commit ae7f04c

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