Ticket #6464: 6464.memcached.incr.decr.2.diff
File 6464.memcached.incr.decr.2.diff, 4.2 KB (added by , 16 years ago) |
---|
-
django/core/cache/backends/base.py
63 63 """ 64 64 return self.get(key) is not None 65 65 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 66 91 __contains__ = has_key -
django/core/cache/backends/dummy.py
18 18 def delete(self, *args, **kwargs): 19 19 pass 20 20 21 def incr(self, key, delta=1): 22 raise NotImplementedException 23 24 def decr(self, key, delta=1): 25 raise NotImplementedException 26 21 27 def get_many(self, *args, **kwargs): 22 28 return {} 23 29 -
django/core/cache/backends/memcached.py
39 39 40 40 def get_many(self, keys): 41 41 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
62 62 self.assertEqual("hello2" in cache, True) 63 63 self.assertEqual("goodbye2" in cache, False) 64 64 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 65 81 def test_data_types(self): 66 82 stuff = { 67 83 'string' : 'this is a string', -
docs/cache.txt
394 394 >>> cache.get('add_key') 395 395 'Initial value' 396 396 397 ***New in Django development verison:** To increment or decrement a key that already 398 exists, use the ``incr()`` or ``decr()`` methods, respectively. These methods are only 399 atomic for the memecached backend. A dummy implementation is available for other cache 400 backends, but ***these operations are not atomic for other cache backends.** Attempting 401 to increment or decrement a nonexistent key will trigger a ValueError exception. The 402 result 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 397 412 There's also a ``get_many()`` interface that only hits the cache once. ``get_many()`` 398 413 returns a dictionary with all the keys you asked for that actually exist in the 399 414 cache (and haven't expired)::