Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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.
  • Loading branch information...
commit 79dd751b0b9e428bf386d7304f442fa9815fdbba 1 parent 2cc1884
Claude Paroz claudep authored
6 django/core/cache/backends/memcached.py
View
@@ -90,6 +90,9 @@ def close(self, **kwargs):
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 @@ def incr(self, key, delta=1, version=None):
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)
2  tests/regressiontests/cache/tests.py
View
@@ -257,6 +257,7 @@ def test_incr(self):
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 @@ def test_decr(self):
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):
Please sign in to comment.
Something went wrong with that request. Please try again.