Ticket #14299: django-cache-add_many.diff
File django-cache-add_many.diff, 7.9 KB (added by , 14 years ago) |
---|
-
django/core/cache/backends/base.py
100 100 # if a subclass overrides it. 101 101 return self.has_key(key) 102 102 103 def add_many(self, data, timeout=None): 104 """ 105 Set a bunch of values in the cache at once from a dict of key/value 106 pairs. For certain backends (memcached), this is much more efficient 107 than calling add() multiple times. 108 109 If timeout is given, that timeout will be used for the key; otherwise 110 the default cache timeout will be used. 111 112 Returns dict() of key/bool pairs indicating whether or not the values 113 were stored. 114 """ 115 results = {} 116 for key, value in data.items(): 117 results[key] = self.add(key, value, timeout) 118 return results 119 120 def offset_many(self, data): 121 """ 122 Add/Subtract a bunch of values in the cache at once from a dict of 123 key/delta pairs. For certain backends (memcached), this is much more 124 efficient than calling incr()/decr() multiple times. 125 126 Returna dict() of key/value pairs containing the new values. Value 127 is False if the key did not exist. 128 """ 129 results = {} 130 for key, value in data.items(): 131 try: 132 results[key] = self.incr(key, value) 133 except ValueError: 134 results[key] = False 135 return results 136 103 137 def set_many(self, data, timeout=None): 104 138 """ 105 139 Set a bunch of values in the cache at once from a dict of key/value -
django/core/cache/backends/dummy.py
27 27 self.validate_key(key) 28 28 return False 29 29 30 def add_many(self, *args, **kwargs): 31 pass 32 30 33 def set_many(self, *args, **kwargs): 31 34 pass 32 35 -
django/core/cache/backends/memcached.py
63 63 self._cache.disconnect_all() 64 64 65 65 def incr(self, key, delta=1): 66 # memcached doesn't support a negative delta 67 if delta < 0: 68 return self.decr(key, -delta) 66 69 try: 67 val = self._cache.incr( key, delta)70 val = self._cache.incr(smart_str(key), delta) 68 71 69 72 # python-memcache responds to incr on non-existent keys by 70 73 # raising a ValueError. Cmemcache returns None. In both … … 77 80 return val 78 81 79 82 def decr(self, key, delta=1): 83 # memcached doesn't support a negative delta 84 if delta < 0: 85 return self.incr(key, -delta) 80 86 try: 81 val = self._cache.decr( key, delta)87 val = self._cache.decr(smart_str(key), delta) 82 88 83 89 # python-memcache responds to decr on non-existent keys by 84 90 # raising a ValueError. Cmemcache returns None. In both -
docs/topics/cache.txt
594 594 595 595 Like ``cache.set()``, ``set_many()`` takes an optional ``timeout`` parameter. 596 596 597 .. versionadded:: 1.3 598 599 To add multiple values more efficiently, use ``add_many()`` to pass a dictionary 600 of key-value pairs. ``add_many()`` returns a dictionary with all the keys passed 601 and their values being a bool indicating whether or not the value was added:: 602 603 >>> cache.set('a', 1) 604 >>> cache.add_many({'a': 2, 'b': 1}) 605 {'a': False, 'b': 1} 606 597 607 You can delete keys explicitly with ``delete()``. This is an easy way of 598 608 clearing the cache for a particular object:: 599 609 … … 641 651 However, if the backend doesn't natively provide an increment/decrement 642 652 operation, it will be implemented using a two-step retrieve/update. 643 653 654 .. versionadded:: 1.3 655 656 To increment and decrement multiple values more efficiently, use 657 ``offset_many()`` to pass a dictionary of key-value pairs, where the value is 658 the delta you would like apply to the existing value. ``offset_many()`` 659 returns a dictionary with all the keys passed and their new value. If a key is 660 not found or there was an error, its value will be False:: 661 662 >>> cache.set_many({'a': 10, 'b': 10}) 663 >>> cache.offset_many({'a': 1, 'b': -1, 'c': 1}) 664 {'a': 11, 'b': 9, 'c': False} 665 666 667 644 668 Cache key warnings 645 669 ------------------ 646 670 -
tests/regressiontests/cache/tests.py
132 132 self.cache.set(key, value) 133 133 self.assertEqual(self.cache.get(key), None) 134 134 135 def test_add_many(self): 136 "add_many does nothing for the dummy cache backend" 137 self.cache.add_many({'a': 1, 'b': 2}) 138 139 def test_offset_many(self): 140 "offset_many does nothing for the dummy cache backend" 141 results = self.cache.offset_many({'a': 1, 'b': -1}) 142 self.assertEqual(results['a'], False) 143 self.assertEqual(results['b'], False) 144 135 145 def test_set_many(self): 136 146 "set_many does nothing for the dummy cache backend" 137 147 self.cache.set_many({'a': 1, 'b': 2}) … … 206 216 self.assertEqual(self.cache.incr('answer', 10), 52) 207 217 self.assertEqual(self.cache.get('answer'), 52) 208 218 self.assertRaises(ValueError, self.cache.incr, 'does_not_exist') 219 self.assertEqual(self.cache.incr('answer', -10), 42) 209 220 210 221 def test_decr(self): 211 222 # Cache values can be decremented … … 215 226 self.assertEqual(self.cache.decr('answer', 10), 32) 216 227 self.assertEqual(self.cache.get('answer'), 32) 217 228 self.assertRaises(ValueError, self.cache.decr, 'does_not_exist') 229 self.assertEqual(self.cache.decr('answer', -10), 42) 218 230 219 231 def test_data_types(self): 220 232 # Many different data types can be cached … … 306 318 self.assertEqual(compressed_value, compressed_result) 307 319 self.assertEqual(value, decompress(compressed_result)) 308 320 321 def test_add_many(self): 322 # Multiple keys can be added using add_many 323 data = {"addmany1": "one", "addmany2": "two"} 324 self.cache.add_many(data) 325 results = self.cache.add_many({"addmany1": "a", "addmany2": "b"}) 326 for key, result in results.items(): 327 self.assertEqual(result, False) 328 self.assertEqual(self.cache.get(key), data.get(key)) 329 330 def test_add_many_expiration(self): 331 # add_many takes a second ``timeout`` parameter 332 333 # ensure keys do not exist, otherwise timeout may not be set 334 self.cache.delete_many(["addmany1", "addmany2"]) 335 self.cache.add_many({"addmany1": "one", "addmany2": "two"}, 1) 336 time.sleep(2) 337 self.assertEqual(self.cache.get("addmany1"), None) 338 self.assertEqual(self.cache.get("addmany2"), None) 339 340 def test_offset_many(self): 341 # Multiple keys can be incremented or decremented using offset_many 342 self.cache.set("offset1", 10) 343 self.cache.set("offset2", 10) 344 self.cache.delete("offset3") 345 results = self.cache.offset_many({ 346 "offset1" : 1, 347 "offset2" : -1, 348 "offset3" : 1, 349 }) 350 self.assertEqual(results.get("offset1"), 11) 351 self.assertEqual(results.get("offset2"), 9) 352 self.assertEqual(results.get("offset3"), False) 353 309 354 def test_set_many(self): 310 355 # Multiple keys can be set using set_many 311 356 self.cache.set_many({"key1": "spam", "key2": "eggs"})