Ticket #6086: simple_cache_deprecate.diff
File simple_cache_deprecate.diff, 6.2 KB (added by , 17 years ago) |
---|
-
django/core/cache/__init__.py
22 22 BACKENDS = { 23 23 # name for use in settings file --> name of module in "backends" directory 24 24 'memcached': 'memcached', 25 'simple': 'simple',26 25 'locmem': 'locmem', 27 26 'file': 'filebased', 28 27 'db': 'db', 29 28 'dummy': 'dummy', 30 29 } 31 30 31 DEPRECATED_BACKENDS = { 32 # deprecated backend --> replacement module 33 'simple': 'locmem', 34 } 35 32 36 def get_cache(backend_uri): 33 37 if backend_uri.find(':') == -1: 34 38 raise InvalidCacheBackendError, "Backend URI must start with scheme://" 35 39 scheme, rest = backend_uri.split(':', 1) 36 40 if not rest.startswith('//'): 37 41 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] 38 47 if scheme not in BACKENDS: 39 48 raise InvalidCacheBackendError, "%r is not a valid cache backend" % scheme 40 49 -
django/core/cache/backends/locmem.py
6 6 except ImportError: 7 7 import pickle 8 8 9 from django.core.cache.backends. simple import CacheClass as SimpleCacheClass9 from django.core.cache.backends.base import BaseCache 10 10 from django.utils.synch import RWLock 11 11 12 class CacheClass(SimpleCacheClass): 13 def __init__(self, host, params): 14 SimpleCacheClass.__init__(self, host, params) 12 class 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 15 30 self._lock = RWLock() 16 31 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 17 41 def add(self, key, value, timeout=None): 18 42 self._lock.writer_enters() 19 43 # Python 2.3 and 2.4 don't allow combined try-except-finally blocks. 20 44 try: 21 45 try: 22 s uper(CacheClass, self).add(key, pickle.dumps(value), timeout)46 self._add(key, pickle.dumps(value), timeout) 23 47 except pickle.PickleError: 24 48 pass 25 49 finally: … … 51 75 finally: 52 76 self._lock.writer_leaves() 53 77 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 54 86 def set(self, key, value, timeout=None): 55 87 self._lock.writer_enters() 56 88 # Python 2.3 and 2.4 don't allow combined try-except-finally blocks. 57 89 try: 58 90 try: 59 s uper(CacheClass, self).set(key, pickle.dumps(value), timeout)91 self._set(key, pickle.dumps(value), timeout) 60 92 except pickle.PickleError: 61 93 pass 62 94 finally: 63 95 self._lock.writer_leaves() 64 96 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 65 119 def delete(self, key): 66 120 self._lock.writer_enters() 67 121 try: 68 SimpleCacheClass.delete(self,key)122 self._delete(key) 69 123 finally: 70 124 self._lock.writer_leaves() -
django/core/cache/backends/filebased.py
1 1 "File-based cache backend" 2 2 3 from django.core.cache.backends.simple import CacheClass as SimpleCacheClass4 from django.utils.http import urlquote_plus5 3 import os, time 6 4 try: 7 5 import cPickle as pickle 8 6 except ImportError: 9 7 import pickle 8 from django.core.cache.backends.base import BaseCache 9 from django.utils.http import urlquote_plus 10 10 11 class CacheClass( SimpleCacheClass):11 class CacheClass(BaseCache): 12 12 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 13 27 self._dir = dir 14 28 if not os.path.exists(self._dir): 15 29 self._createdir() 16 SimpleCacheClass.__init__(self, dir, params)17 del self._cache18 del self._expire_info19 30 20 31 def add(self, key, value, timeout=None): 21 32 fname = self._key_to_file(key)