Ticket #6464: 6464.memcached.incr.decr.2.diff

File 6464.memcached.incr.decr.2.diff, 4.2 KB (added by mmalone, 7 years ago)

Increment and decrement operations for cache backends

  • django/core/cache/backends/base.py

     
    6363        """
    6464        return self.get(key) is not None
    6565
     66    def _incr_or_decr(self, key, delta=1):
     67        '''
     68        Increment or decrement a value in the cache.
     69        '''
     70        if key not in self:
     71            raise ValueError, 'key not found %s' % key
     72        new_value = self.get(key) + delta
     73        self.set(key, new_value)
     74        return new_value
     75
     76
     77    def incr(self, key, delta=1):
     78        """
     79        Add delta to value in the cache. If the key does not exist, raise a
     80        ValueError exception.
     81        """
     82        return self._incr_or_decr(key, delta)
     83
     84    def decr(self, key, delta=1):
     85        """
     86        Subtract delta from value in the cache. If the key does not exist, raise
     87        a ValueError exception.
     88        """
     89        return self._incr_or_decr(key, -delta)
     90
    6691    __contains__ = has_key
  • django/core/cache/backends/dummy.py

     
    1818    def delete(self, *args, **kwargs):
    1919        pass
    2020
     21    def incr(self, key, delta=1):
     22        raise NotImplementedException
     23
     24    def decr(self, key, delta=1):
     25        raise NotImplementedException
     26
    2127    def get_many(self, *args, **kwargs):
    2228        return {}
    2329
  • django/core/cache/backends/memcached.py

     
    3939
    4040    def get_many(self, keys):
    4141        return self._cache.get_multi(map(smart_str,keys))
     42
     43    def decr(self, key, delta=1):
     44        return self._cache.decr(key, delta)
     45
     46    def incr(self, key, delta=1):
     47        return self._cache.incr(key, delta)
  • tests/regressiontests/cache/tests.py

     
    6262        self.assertEqual("hello2" in cache, True)
    6363        self.assertEqual("goodbye2" in cache, False)
    6464
     65    def test_incr(self):
     66        cache.set('answer', 41)
     67        self.assertEqual(cache.incr('answer'), 42)
     68        self.assertEqual(cache.get('answer'), 42)
     69        self.assertEqual(cache.incr('answer', 10), 52)
     70        self.assertEqual(cache.get('answer'), 52)
     71        self.assertRaises(ValueError, cache.incr, 'does_not_exist')
     72
     73    def test_decr(self):
     74        cache.set('answer', 43)
     75        self.assertEqual(cache.decr('answer'), 42)
     76        self.assertEqual(cache.get('answer'), 42)
     77        self.assertEqual(cache.decr('answer', 10), 32)
     78        self.assertEqual(cache.get('answer'), 32)
     79        self.assertRaises(ValueError, cache.decr, 'does_not_exist')
     80
    6581    def test_data_types(self):
    6682        stuff = {
    6783            'string'    : 'this is a string',
  • docs/cache.txt

     
    394394    >>> cache.get('add_key')
    395395    'Initial value'
    396396
     397***New in Django development verison:** To increment or decrement a key that already
     398exists, use the ``incr()`` or ``decr()`` methods, respectively. These methods are only
     399atomic for the memecached backend. A dummy implementation is available for other cache
     400backends, but ***these operations are not atomic for other cache backends.** Attempting
     401to increment or decrement a nonexistent key will trigger a ValueError exception. The
     402result of incrementing or decrementing non-integer values is undefined (don't do that)::
     403
     404    >>> cache.set('num', 1)
     405    >>> cache.incr('num')
     406    2
     407    >>> cache.incr('num', 10)
     408    12
     409    >>> cache.decr('num', 5)
     410    7
     411
    397412There's also a ``get_many()`` interface that only hits the cache once. ``get_many()``
    398413returns a dictionary with all the keys you asked for that actually exist in the
    399414cache (and haven't expired)::
Back to Top