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
This commit is contained in:
Gary Wilson Jr 2007-07-15 06:24:54 +00:00
parent 208352e5d7
commit ae7f04caab
3 changed files with 21 additions and 10 deletions

View File

@ -237,6 +237,7 @@ answer newbie questions, and generally made Django that much better:
Vasiliy Stavenko <stavenko@gmail.com> Vasiliy Stavenko <stavenko@gmail.com>
Thomas Steinacher <http://www.eggdrop.ch/> Thomas Steinacher <http://www.eggdrop.ch/>
nowell strite nowell strite
Sundance
Radek Švarz <http://www.svarz.cz/translate/> Radek Švarz <http://www.svarz.cz/translate/>
Swaroop C H <http://www.swaroopch.info> Swaroop C H <http://www.swaroopch.info>
Aaron Swartz <http://www.aaronsw.com/> Aaron Swartz <http://www.aaronsw.com/>

View File

@ -2,7 +2,11 @@
from django.core.cache.backends.simple import CacheClass as SimpleCacheClass from django.core.cache.backends.simple import CacheClass as SimpleCacheClass
from django.utils.synch import RWLock from django.utils.synch import RWLock
import copy, time import time
try:
import cPickle as pickle
except ImportError:
import pickle
class CacheClass(SimpleCacheClass): class CacheClass(SimpleCacheClass):
def __init__(self, host, params): def __init__(self, host, params):
@ -20,7 +24,10 @@ class CacheClass(SimpleCacheClass):
elif exp < now: elif exp < now:
should_delete = True should_delete = True
else: else:
return copy.deepcopy(self._cache[key]) try:
return pickle.loads(self._cache[key])
except pickle.PickleError:
return default
finally: finally:
self._lock.reader_leaves() self._lock.reader_leaves()
if should_delete: if should_delete:
@ -35,7 +42,10 @@ class CacheClass(SimpleCacheClass):
def set(self, key, value, timeout=None): def set(self, key, value, timeout=None):
self._lock.writer_enters() self._lock.writer_enters()
try: try:
SimpleCacheClass.set(self, key, value, timeout) try:
super(CacheClass, self).set(key, pickle.dumps(value), timeout)
except pickle.PickleError:
pass
finally: finally:
self._lock.writer_leaves() self._lock.writer_leaves()

View File

@ -52,7 +52,6 @@ class Cache(unittest.TestCase):
self.assertEqual("goodbye" in cache, False) self.assertEqual("goodbye" in cache, False)
def test_data_types(self): def test_data_types(self):
# test data types
stuff = { stuff = {
'string' : 'this is a string', 'string' : 'this is a string',
'int' : 42, 'int' : 42,
@ -61,6 +60,7 @@ class Cache(unittest.TestCase):
'dict' : {'A': 1, 'B' : 2}, 'dict' : {'A': 1, 'B' : 2},
'function' : f, 'function' : f,
'class' : C, 'class' : C,
'iter' : iter([1, 2 ,3]),
} }
for (key, value) in stuff.items(): for (key, value) in stuff.items():
cache.set(key, value) cache.set(key, value)