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.
This commit is contained in:
Claude Paroz 2012-11-06 12:19:14 +01:00
parent 2cc1884383
commit 79dd751b0b
2 changed files with 8 additions and 0 deletions

View File

@ -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)

View File

@ -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):