Ticket #7744: 7744-1.diff

File 7744-1.diff, 4.5 KB (added by Matt McClanahan, 16 years ago)
  • django/core/cache/backends/locmem.py

     
    3131
    3232    def add(self, key, value, timeout=None):
    3333        self._lock.writer_enters()
     34        success = False
    3435        try:
    3536            exp = self._expire_info.get(key)
    3637            if exp is None or exp <= time.time():
    3738                try:
    3839                    self._set(key, pickle.dumps(value), timeout)
     40                    success = True
    3941                except pickle.PickleError:
    4042                    pass
    4143        finally:
    4244            self._lock.writer_leaves()
     45        return success
    4346
    4447    def get(self, key, default=None):
    4548        self._lock.reader_enters()
  • django/core/cache/backends/filebased.py

     
    3030
    3131    def add(self, key, value, timeout=None):
    3232        if self.has_key(key):
    33             return None
     33            return False
    3434       
    3535        self.set(key, value, timeout)
     36        return True
    3637
    3738    def get(self, key, default=None):
    3839        fname = self._key_to_file(key)
  • django/core/cache/backends/db.py

     
    4444        return self._base_set('add', key, value, timeout)
    4545
    4646    def _base_set(self, mode, key, value, timeout=None):
     47        inserted = False
    4748        if timeout is None:
    4849            timeout = self.default_timeout
    4950        cursor = connection.cursor()
     
    6061                cursor.execute("UPDATE %s SET value = %%s, expires = %%s WHERE cache_key = %%s" % self._table, [encoded, str(exp), key])
    6162            else:
    6263                cursor.execute("INSERT INTO %s (cache_key, value, expires) VALUES (%%s, %%s, %%s)" % self._table, [key, encoded, str(exp)])
     64                inserted = True
    6365        except DatabaseError:
    6466            # To be threadsafe, updates/inserts are allowed to fail silently
    6567            pass
    6668        else:
    6769            transaction.commit_unless_managed()
     70        if mode == 'add':
     71            return inserted
    6872
    6973    def delete(self, key):
    7074        cursor = connection.cursor()
  • django/core/cache/backends/memcached.py

     
    1717        self._cache = memcache.Client(server.split(';'))
    1818
    1919    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)
    2121
    2222    def get(self, key, default=None):
    2323        val = self._cache.get(smart_str(key))
  • tests/regressiontests/cache/tests.py

     
    2424
    2525    def test_add(self):
    2626        # 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)
    2931        self.assertEqual(cache.get("addkey1"), "value")
    30        
     32
    3133    def test_non_existent(self):
    3234        # get with non-existent keys
    3335        self.assertEqual(cache.get("does_not_exist"), None)
  • docs/cache.txt

     
    387387
    388388**New in Django development version:** To add a key only if it doesn't already
    389389exist, 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::
     390it will not attempt to update the cache if the key specified is already
     391present.  If the key is already present, ``add()`` returns False::
    391392
    392393    >>> cache.set('add_key', 'Initial value')
    393394    >>> cache.add('add_key', 'New value')
     395    False
    394396    >>> cache.get('add_key')
    395397    'Initial value'
    396398
Back to Top