Django

Code

Changeset 8278

Show
Ignore:
Timestamp:
08/09/08 22:52:21 (3 months ago)
Author:
mtredinnick
Message:

Added a return value to the add() method for caches. It's now possible to tell
if a call to add() ended up storing something in the cache.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/core/cache/backends/base.py

    r8084 r8278  
    2020        timeout is given, that timeout will be used for the key; otherwise 
    2121        the default cache timeout will be used. 
     22 
     23        Returns True if the value was stored, False otherwise. 
    2224        """ 
    2325        raise NotImplementedError 
  • django/trunk/django/core/cache/backends/db.py

    r6589 r8278  
    3939 
    4040    def set(self, key, value, timeout=None): 
    41         return self._base_set('set', key, value, timeout) 
     41        self._base_set('set', key, value, timeout) 
    4242 
    4343    def add(self, key, value, timeout=None): 
     
    6363        except DatabaseError: 
    6464            # To be threadsafe, updates/inserts are allowed to fail silently 
    65             pass 
     65            return False 
    6666        else: 
    6767            transaction.commit_unless_managed() 
     68            return True 
    6869 
    6970    def delete(self, key): 
  • django/trunk/django/core/cache/backends/dummy.py

    r6572 r8278  
    88 
    99    def add(self, *args, **kwargs): 
    10         pass 
     10        return True 
    1111 
    1212    def get(self, key, default=None): 
  • django/trunk/django/core/cache/backends/filebased.py

    r8193 r8278  
    3333    def add(self, key, value, timeout=None): 
    3434        if self.has_key(key): 
    35             return Non
     35            return Fals
    3636 
    3737        self.set(key, value, timeout) 
     38        return True 
    3839 
    3940    def get(self, key, default=None): 
  • django/trunk/django/core/cache/backends/locmem.py

    r8090 r8278  
    3737                try: 
    3838                    self._set(key, pickle.dumps(value), timeout) 
     39                    return True 
    3940                except pickle.PickleError: 
    4041                    pass 
     42            return False 
    4143        finally: 
    4244            self._lock.writer_leaves() 
  • django/trunk/django/core/cache/backends/memcached.py

    r6572 r8278  
    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): 
  • django/trunk/docs/cache.txt

    r8261 r8278  
    411411    'Initial value' 
    412412 
    413 There's also a ``get_many()`` interface that only hits the cache once. ``get_many()`` 
    414 returns a dictionary with all the keys you asked for that actually exist in the 
    415 cache (and haven't expired):: 
     413If you need to know whether ``add()`` stored a value in the cache, you can 
     414check the return value. It will return ``True`` if the value was stored, 
     415``False`` otherwise. 
     416 
     417There's also a ``get_many()`` interface that only hits the cache once. 
     418``get_many()`` returns a dictionary with all the keys you asked for that 
     419actually exist in the cache (and haven't expired):: 
    416420 
    417421    >>> cache.set('a', 1) 
  • django/trunk/tests/regressiontests/cache/tests.py

    r8193 r8278  
    3232        # test add (only add if key isn't already in cache) 
    3333        cache.add("addkey1", "value") 
    34         cache.add("addkey1", "newvalue") 
     34        result = cache.add("addkey1", "newvalue") 
     35        self.assertEqual(result, False) 
    3536        self.assertEqual(cache.get("addkey1"), "value") 
    3637