Commit f6670e13 authored by Malcolm Tredinnick's avatar Malcolm Tredinnick
Browse files

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.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@8278 bcc190cf-cafb-0310-a4f2-bffc1f526a37
parent 38dc4727
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -19,6 +19,8 @@ class BaseCache(object):
        Set a value in the cache if the key does not already exist. If
        timeout is given, that timeout will be used for the key; otherwise
        the default cache timeout will be used.

        Returns True if the value was stored, False otherwise.
        """
        raise NotImplementedError

+3 −2
Original line number Diff line number Diff line
@@ -38,7 +38,7 @@ class CacheClass(BaseCache):
        return pickle.loads(base64.decodestring(row[1]))

    def set(self, key, value, timeout=None):
        return self._base_set('set', key, value, timeout)
        self._base_set('set', key, value, timeout)

    def add(self, key, value, timeout=None):
        return self._base_set('add', key, value, timeout)
@@ -62,9 +62,10 @@ class CacheClass(BaseCache):
                cursor.execute("INSERT INTO %s (cache_key, value, expires) VALUES (%%s, %%s, %%s)" % self._table, [key, encoded, str(exp)])
        except DatabaseError:
            # To be threadsafe, updates/inserts are allowed to fail silently
            pass
            return False
        else:
            transaction.commit_unless_managed()
            return True

    def delete(self, key):
        cursor = connection.cursor()
+1 −1
Original line number Diff line number Diff line
@@ -7,7 +7,7 @@ class CacheClass(BaseCache):
        pass

    def add(self, *args, **kwargs):
        pass
        return True

    def get(self, key, default=None):
        return default
+2 −1
Original line number Diff line number Diff line
@@ -32,9 +32,10 @@ class CacheClass(BaseCache):

    def add(self, key, value, timeout=None):
        if self.has_key(key):
            return None
            return False

        self.set(key, value, timeout)
        return True

    def get(self, key, default=None):
        fname = self._key_to_file(key)
+2 −0
Original line number Diff line number Diff line
@@ -36,8 +36,10 @@ class CacheClass(BaseCache):
            if exp is None or exp <= time.time():
                try:
                    self._set(key, pickle.dumps(value), timeout)
                    return True
                except pickle.PickleError:
                    pass
            return False
        finally:
            self._lock.writer_leaves()

Loading