Ticket #6131: locmem-fix.patch

File locmem-fix.patch, 2.1 KB (added by Brian Johnson, 11 years ago)
  • django/core/cache/backends/locmem.py

    diff --git a/django/core/cache/backends/locmem.py b/django/core/cache/backends/locmem.py
    index 2734cef..2b807f0 100644
    a b class CacheClass(BaseCache): 
    2929
    3030        self._lock = RWLock()
    3131
    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 
    4132    def add(self, key, value, timeout=None):
    4233        self._lock.writer_enters()
    43         # Python 2.3 and 2.4 don't allow combined try-except-finally blocks.
    4434        try:
    45             try:
    46                 self._add(key, pickle.dumps(value), timeout)
    47             except pickle.PickleError:
    48                 pass
     35            now = time.time()
     36            exp = self._expire_info.get(key)
     37            if exp is None or exp < now:
     38                try:
     39                    self._set(key, pickle.dumps(value), timeout)
     40                except pickle.PickleError:
     41                    pass
    4942        finally:
    5043            self._lock.writer_leaves()
    5144
    class CacheClass(BaseCache): 
    9588            self._lock.writer_leaves()
    9689
    9790    def has_key(self, key):
    98         return key in self._cache
     91        should_delete = False
     92        self._lock.reader_enters()
     93        try:
     94            now = time.time()
     95            exp = self._expire_info.get(key)
     96            if exp is None:
     97                return False
     98            elif exp < now:
     99                should_delete = True
     100            else:
     101                return True
     102        finally:
     103            self._lock.reader_leaves()
     104        if should_delete:
     105            self._lock.writer_enters()
     106            try:
     107                del self._cache[key]
     108                del self._expire_info[key]
     109                return False
     110            finally:
     111                self._lock.writer_leaves()
    99112
    100113    def _cull(self):
    101114        if self._cull_frequency == 0:
Back to Top