Ticket #6086: 6086_combined_patch.diff

File 6086_combined_patch.diff, 9.6 KB (added by mcroydon, 17 years ago)

Updating combined diff to include rm_simple.diff (patch with -p1)

  • 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  
    293293
    294294# The cache backend to use.  See the docstring in django.core.cache for the
    295295# possible values.
    296 CACHE_BACKEND = 'simple://'
     296CACHE_BACKEND = 'locmem://'
    297297CACHE_MIDDLEWARE_KEY_PREFIX = ''
    298298CACHE_MIDDLEWARE_SECONDS = 600
    299299
  • 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  
    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/filebased.py

    diff --git a/django/core/cache/backends/filebased.py b/django/core/cache/backends/filebased.py
    index 690193a..72cb877 100644
    a b  
    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)
  • 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:  
    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:
    class CacheClass(SimpleCacheClass):  
    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/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 BaseCache
    4 import time
    5 
    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 = 300
    17 
    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 = 3
    23 
    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_timeout
    29         if key not in self._cache.keys():
    30             self._cache[key] = value
    31             self._expire_info[key] = time.time() + timeout
    32 
    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 default
    38         elif exp < now:
    39             del self._cache[key]
    40             del self._expire_info[key]
    41             return default
    42         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_timeout
    50         self._cache[key] = value
    51         self._expire_info[key] = time.time() + timeout
    52 
    53     def delete(self, key):
    54         try:
    55             del self._cache[key]
    56         except KeyError:
    57             pass
    58         try:
    59             del self._expire_info[key]
    60         except KeyError:
    61             pass
    62 
    63     def has_key(self, key):
    64         return key in self._cache
    65 
    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):  
    7272            'function'  : f,
    7373            'class'     : C,
    7474        }
     75        cache.set("stuff", stuff)
     76        self.assertEqual(cache.get("stuff"), stuff)
    7577
    7678    def test_expiration(self):
    7779        # expiration
Back to Top