diff --git a/django/core/cache/backends/locmem.py b/django/core/cache/backends/locmem.py
index cab690b..73b02a5 100644
|
a
|
b
|
class LocMemCache(BaseCache):
|
| 27 | 27 | def add(self, key, value, timeout=DEFAULT_TIMEOUT, version=None): |
| 28 | 28 | key = self.make_key(key, version=version) |
| 29 | 29 | self.validate_key(key) |
| 30 | | try: |
| 31 | | pickled = pickle.dumps(value, pickle.HIGHEST_PROTOCOL) |
| 32 | | except pickle.PickleError: |
| 33 | | return False |
| | 30 | pickled = pickle.dumps(value, pickle.HIGHEST_PROTOCOL) |
| 34 | 31 | with self._lock.writer(): |
| 35 | 32 | exp = self._expire_info.get(key) |
| 36 | 33 | if exp is None or exp <= time.time(): |
| … |
… |
class LocMemCache(BaseCache):
|
| 69 | 66 | def set(self, key, value, timeout=DEFAULT_TIMEOUT, version=None): |
| 70 | 67 | key = self.make_key(key, version=version) |
| 71 | 68 | self.validate_key(key) |
| 72 | | try: |
| 73 | | pickled = pickle.dumps(value, pickle.HIGHEST_PROTOCOL) |
| 74 | | except pickle.PickleError: |
| 75 | | pass |
| 76 | | else: |
| 77 | | with self._lock.writer(): |
| 78 | | self._set(key, pickled, timeout) |
| | 69 | pickled = pickle.dumps(value, pickle.HIGHEST_PROTOCOL) |
| | 70 | with self._lock.writer(): |
| | 71 | self._set(key, pickled, timeout) |
| 79 | 72 | |
| 80 | 73 | def incr(self, key, delta=1, version=None): |
| 81 | 74 | value = self.get(key, version=version) |
| … |
… |
class LocMemCache(BaseCache):
|
| 83 | 76 | raise ValueError("Key '%s' not found" % key) |
| 84 | 77 | new_value = value + delta |
| 85 | 78 | key = self.make_key(key, version=version) |
| 86 | | try: |
| 87 | | pickled = pickle.dumps(new_value, pickle.HIGHEST_PROTOCOL) |
| 88 | | except pickle.PickleError: |
| 89 | | pass |
| 90 | | else: |
| 91 | | with self._lock.writer(): |
| 92 | | self._cache[key] = pickled |
| | 79 | pickled = pickle.dumps(new_value, pickle.HIGHEST_PROTOCOL) |
| | 80 | with self._lock.writer(): |
| | 81 | self._cache[key] = pickled |
| 93 | 82 | return new_value |
| 94 | 83 | |
| 95 | 84 | def has_key(self, key, version=None): |
diff --git a/tests/cache/tests.py b/tests/cache/tests.py
index 89c53da..c66d0f8 100644
|
a
|
b
|
from __future__ import unicode_literals
|
| 6 | 6 | |
| 7 | 7 | import hashlib |
| 8 | 8 | import os |
| 9 | | import pickle |
| | 9 | try: # Use the same idiom as in cache backends |
| | 10 | from django.utils.six.moves import cPickle as pickle |
| | 11 | except ImportError: |
| | 12 | import pickle |
| 10 | 13 | import random |
| 11 | 14 | import re |
| 12 | 15 | import string |
| … |
… |
class C:
|
| 48 | 51 | def m(n): |
| 49 | 52 | return 24 |
| 50 | 53 | |
| | 54 | class Unpickable(object): |
| | 55 | def __getstate__(self): |
| | 56 | raise pickle.PickleError() |
| | 57 | |
| 51 | 58 | |
| 52 | 59 | class DummyCacheTests(unittest.TestCase): |
| 53 | 60 | # The Dummy cache backend doesn't really behave like a test backend, |
| … |
… |
class BaseCacheTests(object):
|
| 823 | 830 | self.assertEqual(get_cache_data.content, content.encode('utf-8')) |
| 824 | 831 | self.assertEqual(get_cache_data.cookies, response.cookies) |
| 825 | 832 | |
| | 833 | def test_add_fail_on_pickleerror(self): |
| | 834 | """See https://code.djangoproject.com/ticket/21200""" |
| | 835 | with self.assertRaises(pickle.PickleError): |
| | 836 | self.cache.add('unpickable', Unpickable()) |
| | 837 | |
| | 838 | def test_set_fail_on_pickleerror(self): |
| | 839 | """See https://code.djangoproject.com/ticket/21200""" |
| | 840 | with self.assertRaises(pickle.PickleError): |
| | 841 | self.cache.set('unpickable', Unpickable()) |
| | 842 | |
| 826 | 843 | def custom_key_func(key, key_prefix, version): |
| 827 | 844 | "A customized cache key function" |
| 828 | 845 | return 'CUSTOM-' + '-'.join([key_prefix, str(version), key]) |