Ticket #6086: simple_cache_deprecate.diff

File simple_cache_deprecate.diff, 6.2 KB (added by Paul Bissex, 17 years ago)
  • django/core/cache/__init__.py

     
    2222BACKENDS = {
    2323    # name for use in settings file --> name of module in "backends" directory
    2424    'memcached': 'memcached',
    25     'simple': 'simple',
    2625    'locmem': 'locmem',
    2726    'file': 'filebased',
    2827    'db': 'db',
    2928    'dummy': 'dummy',
    3029}
    3130
     31DEPRECATED_BACKENDS = {
     32    # deprecated backend --> replacement module
     33    'simple': 'locmem',
     34}
     35
    3236def get_cache(backend_uri):
    3337    if backend_uri.find(':') == -1:
    3438        raise InvalidCacheBackendError, "Backend URI must start with scheme://"
    3539    scheme, rest = backend_uri.split(':', 1)
    3640    if not rest.startswith('//'):
    3741        raise InvalidCacheBackendError, "Backend URI must start with scheme://"
     42    if scheme in DEPRECATED_BACKENDS:
     43        import warnings
     44        warnings.warn("'%s' backend is deprecated. Use '%s' instead." %
     45            (scheme, DEPRECATED_BACKENDS[scheme]), DeprecationWarning)
     46        scheme = DEPRECATED_BACKENDS[scheme]
    3847    if scheme not in BACKENDS:
    3948        raise InvalidCacheBackendError, "%r is not a valid cache backend" % scheme
    4049
  • django/core/cache/backends/locmem.py

     
    66except ImportError:
    77    import pickle
    88
    9 from django.core.cache.backends.simple import CacheClass as SimpleCacheClass
     9from django.core.cache.backends.base import BaseCache
    1010from django.utils.synch import RWLock
    1111
    12 class CacheClass(SimpleCacheClass):
    13     def __init__(self, host, params):
    14         SimpleCacheClass.__init__(self, host, params)
     12class CacheClass(BaseCache):
     13    def __init__(self, _, params):
     14        BaseCache.__init__(self, params)
     15        self._cache = {}
     16        self._expire_info = {}
     17
     18        max_entries = params.get('max_entries', 300)
     19        try:
     20            self._max_entries = int(max_entries)
     21        except (ValueError, TypeError):
     22            self._max_entries = 300
     23
     24        cull_frequency = params.get('cull_frequency', 3)
     25        try:
     26            self._cull_frequency = int(cull_frequency)
     27        except (ValueError, TypeError):
     28            self._cull_frequency = 3
     29
    1530        self._lock = RWLock()
    1631
     32    def _add(self, key, value, timeout=None):
     33        if len(self._cache) >= self._max_entries:
     34            self._cull()
     35        if timeout is None:
     36            timeout = self.default_timeout
     37        if key not in self._cache.keys():
     38            self._cache[key] = value
     39            self._expire_info[key] = time.time() + timeout
     40
    1741    def add(self, key, value, timeout=None):
    1842        self._lock.writer_enters()
    1943        # Python 2.3 and 2.4 don't allow combined try-except-finally blocks.
    2044        try:
    2145            try:
    22                 super(CacheClass, self).add(key, pickle.dumps(value), timeout)
     46                self._add(key, pickle.dumps(value), timeout)
    2347            except pickle.PickleError:
    2448                pass
    2549        finally:
     
    5175            finally:
    5276                self._lock.writer_leaves()
    5377
     78    def _set(self, key, value, timeout=None):
     79        if len(self._cache) >= self._max_entries:
     80            self._cull()
     81        if timeout is None:
     82            timeout = self.default_timeout
     83        self._cache[key] = value
     84        self._expire_info[key] = time.time() + timeout
     85
    5486    def set(self, key, value, timeout=None):
    5587        self._lock.writer_enters()
    5688        # Python 2.3 and 2.4 don't allow combined try-except-finally blocks.
    5789        try:
    5890            try:
    59                 super(CacheClass, self).set(key, pickle.dumps(value), timeout)
     91                self._set(key, pickle.dumps(value), timeout)
    6092            except pickle.PickleError:
    6193                pass
    6294        finally:
    6395            self._lock.writer_leaves()
    6496
     97    def has_key(self, key):
     98        return key in self._cache
     99
     100    def _cull(self):
     101        if self._cull_frequency == 0:
     102            self._cache.clear()
     103            self._expire_info.clear()
     104        else:
     105            doomed = [k for (i, k) in enumerate(self._cache) if i % self._cull_frequency == 0]
     106            for k in doomed:
     107                self.delete(k)
     108
     109    def _delete(self, key):
     110        try:
     111            del self._cache[key]
     112        except KeyError:
     113            pass
     114        try:
     115            del self._expire_info[key]
     116        except KeyError:
     117            pass
     118
    65119    def delete(self, key):
    66120        self._lock.writer_enters()
    67121        try:
    68             SimpleCacheClass.delete(self, key)
     122            self._delete(key)
    69123        finally:
    70124            self._lock.writer_leaves()
  • django/core/cache/backends/filebased.py

     
    11"File-based cache backend"
    22
    3 from django.core.cache.backends.simple import CacheClass as SimpleCacheClass
    4 from django.utils.http import urlquote_plus
    53import os, time
    64try:
    75    import cPickle as pickle
    86except ImportError:
    97    import pickle
     8from django.core.cache.backends.base import BaseCache
     9from django.utils.http import urlquote_plus
    1010
    11 class CacheClass(SimpleCacheClass):
     11class CacheClass(BaseCache):
    1212    def __init__(self, dir, params):
     13        BaseCache.__init__(self, params)
     14       
     15        max_entries = params.get('max_entries', 300)
     16        try:
     17            self._max_entries = int(max_entries)
     18        except (ValueError, TypeError):
     19            self._max_entries = 300
     20           
     21        cull_frequency = params.get('cull_frequency', 3)
     22        try:
     23            self._cull_frequency = int(cull_frequency)
     24        except (ValueError, TypeError):
     25            self._cull_frequency = 3
     26           
    1327        self._dir = dir
    1428        if not os.path.exists(self._dir):
    1529            self._createdir()
    16         SimpleCacheClass.__init__(self, dir, params)
    17         del self._cache
    18         del self._expire_info
    1930
    2031    def add(self, key, value, timeout=None):
    2132        fname = self._key_to_file(key)
Back to Top