From e645c89bbbcf62479441e50b5e5fdf48a80d424f Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Tue, 6 Nov 2012 12:19:14 +0100 Subject: [PATCH] [1.5.x] Fixed #14315 -- Made memcached backend handle negative incr/decr values Thanks Michael Manfre for the report and initial patch and Tobias McNulty for the review. Backport of 79dd751b0b from master. --- django/core/cache/backends/memcached.py | 6 ++++++ tests/regressiontests/cache/tests.py | 2 ++ 2 files changed, 8 insertions(+) diff --git a/django/core/cache/backends/memcached.py b/django/core/cache/backends/memcached.py index 9bb47c8344..2c3f198847 100644 --- a/django/core/cache/backends/memcached.py +++ b/django/core/cache/backends/memcached.py @@ -90,6 +90,9 @@ class BaseMemcachedCache(BaseCache): def incr(self, key, delta=1, version=None): key = self.make_key(key, version=version) + # memcached doesn't support a negative delta + if delta < 0: + return self._cache.decr(key, -delta) try: val = self._cache.incr(key, delta) @@ -105,6 +108,9 @@ class BaseMemcachedCache(BaseCache): def decr(self, key, delta=1, version=None): key = self.make_key(key, version=version) + # memcached doesn't support a negative delta + if delta < 0: + return self._cache.incr(key, -delta) try: val = self._cache.decr(key, delta) diff --git a/tests/regressiontests/cache/tests.py b/tests/regressiontests/cache/tests.py index a6eff8950b..9960c01300 100644 --- a/tests/regressiontests/cache/tests.py +++ b/tests/regressiontests/cache/tests.py @@ -257,6 +257,7 @@ class BaseCacheTests(object): self.assertEqual(self.cache.get('answer'), 42) self.assertEqual(self.cache.incr('answer', 10), 52) self.assertEqual(self.cache.get('answer'), 52) + self.assertEqual(self.cache.incr('answer', -10), 42) self.assertRaises(ValueError, self.cache.incr, 'does_not_exist') def test_decr(self): @@ -266,6 +267,7 @@ class BaseCacheTests(object): self.assertEqual(self.cache.get('answer'), 42) self.assertEqual(self.cache.decr('answer', 10), 32) self.assertEqual(self.cache.get('answer'), 32) + self.assertEqual(self.cache.decr('answer', -10), 42) self.assertRaises(ValueError, self.cache.decr, 'does_not_exist') def test_data_types(self):