Ticket #14315: django-memcached-negative-delta.diff

File django-memcached-negative-delta.diff, 1.6 KB (added by manfre, 3 years ago)
  • django/core/cache/backends/memcached.py

    diff --git a/django/core/cache/backends/memcached.py b/django/core/cache/backends/memcached.py
    a b  
    8383
    8484    def incr(self, key, delta=1, version=None):
    8585        key = self.make_key(key, version=version)
     86        # memcached doesn't support a negative delta
     87        if delta < 0:
     88            return self._cache.decr(key, -delta)
    8689        try:
    8790            val = self._cache.incr(key, delta)
    8891
     
    98101
    99102    def decr(self, key, delta=1, version=None):
    100103        key = self.make_key(key, version=version)
     104        if delta < 0:
     105            return self._cache.incr(key, -delta)
    101106        try:
    102107            val = self._cache.decr(key, delta)
    103108
  • tests/regressiontests/cache/tests.py

    diff --git a/tests/regressiontests/cache/tests.py b/tests/regressiontests/cache/tests.py
    a b  
    243243        self.assertEqual(self.cache.incr('answer', 10), 52)
    244244        self.assertEqual(self.cache.get('answer'), 52)
    245245        self.assertRaises(ValueError, self.cache.incr, 'does_not_exist')
     246        self.assertEqual(self.cache.incr('answer', -10), 42)
    246247
    247248    def test_decr(self):
    248249        # Cache values can be decremented
     
    252253        self.assertEqual(self.cache.decr('answer', 10), 32)
    253254        self.assertEqual(self.cache.get('answer'), 32)
    254255        self.assertRaises(ValueError, self.cache.decr, 'does_not_exist')
    255 
     256        self.assertEqual(self.cache.decr('answer', -10), 42)
     257       
    256258    def test_data_types(self):
    257259        # Many different data types can be cached
    258260        stuff = {
Back to Top