Commit 8e8d4b58 authored by Russell Keith-Magee's avatar Russell Keith-Magee
Browse files

Fixed #12671 -- Added set_many(), get_many(), and clear() methods to the cache...

Fixed #12671 -- Added set_many(), get_many(), and clear() methods to the cache backend interface. Thanks to Jeff Balogh for the report and patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@12306 bcc190cf-cafb-0310-a4f2-bffc1f526a37
parent c6ee1f6f
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -56,6 +56,7 @@ answer newbie questions, and generally made Django that much better:
    Mike Axiak <axiak@mit.edu>
    Niran Babalola <niran@niran.org>
    Morten Bagai <m@bagai.com>
    Jeff Balogh <jbalogh@mozilla.com>
    Mikaël Barbero <mikael.barbero nospam at nospam free.fr>
    Randy Barlow <randy@electronsweatshop.com>
    Scott Barr <scott@divisionbyzero.com.au>
+25 −0
Original line number Diff line number Diff line
@@ -91,3 +91,28 @@ class BaseCache(object):
        # so that it always has the same functionality as has_key(), even
        # if a subclass overrides it.
        return self.has_key(key)

    def set_many(self, data, timeout=None):
        """
        Set a bunch of values in the cache at once from a dict of key/value
        pairs.  For certain backends (memcached), this is much more efficient
        than calling set() multiple times.

        If timeout is given, that timeout will be used for the key; otherwise
        the default cache timeout will be used.
        """
        for key, value in data.items():
            self.set(key, value, timeout)

    def delete_many(self, keys):
        """
        Set a bunch of values in the cache at once.  For certain backends
        (memcached), this is much more efficient than calling delete() multiple
        times.
        """
        for key in keys:
            self.delete(key)

    def clear(self):
        """Remove *all* values from the cache at once."""
        raise NotImplementedError
+5 −1
Original line number Diff line number Diff line
@@ -84,7 +84,7 @@ class CacheClass(BaseCache):

    def _cull(self, cursor, now):
        if self._cull_frequency == 0:
            cursor.execute("DELETE FROM %s" % self._table)
            self.clear()
        else:
            cursor.execute("DELETE FROM %s WHERE expires < %%s" % self._table, [str(now)])
            cursor.execute("SELECT COUNT(*) FROM %s" % self._table)
@@ -92,3 +92,7 @@ class CacheClass(BaseCache):
            if num > self._max_entries:
                cursor.execute("SELECT cache_key FROM %s ORDER BY cache_key LIMIT 1 OFFSET %%s" % self._table, [num / self._cull_frequency])
                cursor.execute("DELETE FROM %s WHERE cache_key < %%s" % self._table, [cursor.fetchone()[0]])

    def clear(self):
        cursor = connection.cursor()
        cursor.execute('DELETE FROM %s' % self._table)
+9 −0
Original line number Diff line number Diff line
@@ -23,3 +23,12 @@ class CacheClass(BaseCache):

    def has_key(self, *args, **kwargs):
        return False

    def set_many(self, *args, **kwargs):
        pass

    def delete_many(self, *args, **kwargs):
        pass

    def clear(self):
        pass
+7 −0
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@

import os
import time
import shutil
try:
    import cPickle as pickle
except ImportError:
@@ -150,3 +151,9 @@ class CacheClass(BaseCache):
            count += len(files)
        return count
    _num_entries = property(_get_num_entries)

    def clear(self):
        try:
            shutil.rmtree(self._dir)
        except (IOError, OSError):
            pass
Loading