Ticket #6086: 6086_combined_patch.diff
File 6086_combined_patch.diff, 9.6 KB (added by , 17 years ago) |
---|
-
django/conf/global_settings.py
diff --git a/django/conf/global_settings.py b/django/conf/global_settings.py index 837158a..187eb06 100644
a b SESSION_FILE_PATH = '/tmp/' # Directory to store ses 293 293 294 294 # The cache backend to use. See the docstring in django.core.cache for the 295 295 # possible values. 296 CACHE_BACKEND = ' simple://'296 CACHE_BACKEND = 'locmem://' 297 297 CACHE_MIDDLEWARE_KEY_PREFIX = '' 298 298 CACHE_MIDDLEWARE_SECONDS = 600 299 299 -
django/core/cache/__init__.py
diff --git a/django/core/cache/__init__.py b/django/core/cache/__init__.py index 6da8e88..495cc92 100644
a b from django.core.cache.backends.base import InvalidCacheBackendError 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/filebased.py
diff --git a/django/core/cache/backends/filebased.py b/django/core/cache/backends/filebased.py index 690193a..72cb877 100644
a b 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) -
django/core/cache/backends/locmem.py
diff --git a/django/core/cache/backends/locmem.py b/django/core/cache/backends/locmem.py index 2d74e2b..2734cef 100644
a b try: 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: … … class CacheClass(SimpleCacheClass): 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/simple.py
diff --git a/django/core/cache/backends/simple.py b/django/core/cache/backends/simple.py index ff60d49..e69de29 100644
a b 1 "Single-process in-memory cache backend."2 3 from django.core.cache.backends.base import BaseCache4 import time5 6 class CacheClass(BaseCache):7 def __init__(self, host, params):8 BaseCache.__init__(self, params)9 self._cache = {}10 self._expire_info = {}11 12 max_entries = params.get('max_entries', 300)13 try:14 self._max_entries = int(max_entries)15 except (ValueError, TypeError):16 self._max_entries = 30017 18 cull_frequency = params.get('cull_frequency', 3)19 try:20 self._cull_frequency = int(cull_frequency)21 except (ValueError, TypeError):22 self._cull_frequency = 323 24 def add(self, key, value, timeout=None):25 if len(self._cache) >= self._max_entries:26 self._cull()27 if timeout is None:28 timeout = self.default_timeout29 if key not in self._cache.keys():30 self._cache[key] = value31 self._expire_info[key] = time.time() + timeout32 33 def get(self, key, default=None):34 now = time.time()35 exp = self._expire_info.get(key)36 if exp is None:37 return default38 elif exp < now:39 del self._cache[key]40 del self._expire_info[key]41 return default42 else:43 return self._cache[key]44 45 def set(self, key, value, timeout=None):46 if len(self._cache) >= self._max_entries:47 self._cull()48 if timeout is None:49 timeout = self.default_timeout50 self._cache[key] = value51 self._expire_info[key] = time.time() + timeout52 53 def delete(self, key):54 try:55 del self._cache[key]56 except KeyError:57 pass58 try:59 del self._expire_info[key]60 except KeyError:61 pass62 63 def has_key(self, key):64 return key in self._cache65 66 def _cull(self):67 if self._cull_frequency == 0:68 self._cache.clear()69 self._expire_info.clear()70 else:71 doomed = [k for (i, k) in enumerate(self._cache) if i % self._cull_frequency == 0]72 for k in doomed:73 self.delete(k) -
tests/regressiontests/cache/tests.py
diff --git a/tests/regressiontests/cache/tests.py b/tests/regressiontests/cache/tests.py index e94ea33..9ac2722 100644
a b class Cache(unittest.TestCase): 72 72 'function' : f, 73 73 'class' : C, 74 74 } 75 cache.set("stuff", stuff) 76 self.assertEqual(cache.get("stuff"), stuff) 75 77 76 78 def test_expiration(self): 77 79 # expiration