Ticket #7744: 7744-1.diff
File 7744-1.diff, 4.5 KB (added by , 16 years ago) |
---|
-
django/core/cache/backends/locmem.py
31 31 32 32 def add(self, key, value, timeout=None): 33 33 self._lock.writer_enters() 34 success = False 34 35 try: 35 36 exp = self._expire_info.get(key) 36 37 if exp is None or exp <= time.time(): 37 38 try: 38 39 self._set(key, pickle.dumps(value), timeout) 40 success = True 39 41 except pickle.PickleError: 40 42 pass 41 43 finally: 42 44 self._lock.writer_leaves() 45 return success 43 46 44 47 def get(self, key, default=None): 45 48 self._lock.reader_enters() -
django/core/cache/backends/filebased.py
30 30 31 31 def add(self, key, value, timeout=None): 32 32 if self.has_key(key): 33 return None33 return False 34 34 35 35 self.set(key, value, timeout) 36 return True 36 37 37 38 def get(self, key, default=None): 38 39 fname = self._key_to_file(key) -
django/core/cache/backends/db.py
44 44 return self._base_set('add', key, value, timeout) 45 45 46 46 def _base_set(self, mode, key, value, timeout=None): 47 inserted = False 47 48 if timeout is None: 48 49 timeout = self.default_timeout 49 50 cursor = connection.cursor() … … 60 61 cursor.execute("UPDATE %s SET value = %%s, expires = %%s WHERE cache_key = %%s" % self._table, [encoded, str(exp), key]) 61 62 else: 62 63 cursor.execute("INSERT INTO %s (cache_key, value, expires) VALUES (%%s, %%s, %%s)" % self._table, [key, encoded, str(exp)]) 64 inserted = True 63 65 except DatabaseError: 64 66 # To be threadsafe, updates/inserts are allowed to fail silently 65 67 pass 66 68 else: 67 69 transaction.commit_unless_managed() 70 if mode == 'add': 71 return inserted 68 72 69 73 def delete(self, key): 70 74 cursor = connection.cursor() -
django/core/cache/backends/memcached.py
17 17 self._cache = memcache.Client(server.split(';')) 18 18 19 19 def add(self, key, value, timeout=0): 20 self._cache.add(key.encode('ascii', 'ignore'), value, timeout or self.default_timeout)20 return self._cache.add(key.encode('ascii', 'ignore'), value, timeout or self.default_timeout) 21 21 22 22 def get(self, key, default=None): 23 23 val = self._cache.get(smart_str(key)) -
tests/regressiontests/cache/tests.py
24 24 25 25 def test_add(self): 26 26 # test add (only add if key isn't already in cache) 27 cache.add("addkey1", "value") 28 cache.add("addkey1", "newvalue") 27 ret = cache.add("addkey1", "value") 28 self.assertTrue(ret) 29 ret = cache.add("addkey1", "newvalue") 30 self.assertFalse(ret) 29 31 self.assertEqual(cache.get("addkey1"), "value") 30 32 31 33 def test_non_existent(self): 32 34 # get with non-existent keys 33 35 self.assertEqual(cache.get("does_not_exist"), None) -
docs/cache.txt
387 387 388 388 **New in Django development version:** To add a key only if it doesn't already 389 389 exist, use the ``add()`` method. It takes the same parameters as ``set()``, but 390 it will not attempt to update the cache if the key specified is already present:: 390 it will not attempt to update the cache if the key specified is already 391 present. If the key is already present, ``add()`` returns False:: 391 392 392 393 >>> cache.set('add_key', 'Initial value') 393 394 >>> cache.add('add_key', 'New value') 395 False 394 396 >>> cache.get('add_key') 395 397 'Initial value' 396 398