Author: carljm
Date: 2011-11-26 14:27:16 -0800 (Sat, 26 Nov 2011)
New Revision: 17151

Modified:
   django/trunk/django/core/cache/backends/locmem.py
   django/trunk/tests/regressiontests/cache/tests.py
Log:
Fixed #17287 -- Prevented LocMemCache.incr/decr from changing key expiry time. 
Thanks Ivan Virabyan for report and patch.

Modified: django/trunk/django/core/cache/backends/locmem.py
===================================================================
--- django/trunk/django/core/cache/backends/locmem.py   2011-11-26 08:43:43 UTC 
(rev 17150)
+++ django/trunk/django/core/cache/backends/locmem.py   2011-11-26 22:27:16 UTC 
(rev 17151)
@@ -87,6 +87,22 @@
         finally:
             self._lock.writer_leaves()
 
+    def incr(self, key, delta=1, version=None):
+        value = self.get(key, version=version)
+        if value is None:
+            raise ValueError("Key '%s' not found" % key)
+        new_value = value + delta
+        key = self.make_key(key, version=version)
+        self._lock.writer_enters()
+        try:
+            pickled = pickle.dumps(new_value, pickle.HIGHEST_PROTOCOL)
+            self._cache[key] = pickled
+        except pickle.PickleError:
+            pass
+        finally:
+            self._lock.writer_leaves()
+        return new_value
+
     def has_key(self, key, version=None):
         key = self.make_key(key, version=version)
         self.validate_key(key)

Modified: django/trunk/tests/regressiontests/cache/tests.py
===================================================================
--- django/trunk/tests/regressiontests/cache/tests.py   2011-11-26 08:43:43 UTC 
(rev 17150)
+++ django/trunk/tests/regressiontests/cache/tests.py   2011-11-26 22:27:16 UTC 
(rev 17151)
@@ -865,6 +865,16 @@
         self.assertEqual(mirror_cache.get('value1'), 42)
         self.assertEqual(other_cache.get('value1'), None)
 
+    def test_incr_decr_timeout(self):
+        """incr/decr does not modify expiry time (matches memcached 
behavior)"""
+        key = 'value'
+        _key = self.cache.make_key(key)
+        self.cache.set(key, 1, timeout=self.cache.default_timeout*10)
+        expire = self.cache._expire_info[_key]
+        self.cache.incr(key)
+        self.assertEqual(expire, self.cache._expire_info[_key])
+        self.cache.decr(key)
+        self.assertEqual(expire, self.cache._expire_info[_key])
 
 # memcached backend isn't guaranteed to be available.
 # To check the memcached backend, the test settings file will

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to