Django

Code

Changeset 6904

Show
Ignore:
Timestamp:
12/10/07 19:20:25 (1 year ago)
Author:
mtredinnick
Message:

Fixe #6131 -- Fixed a problem with expired keys in the locmem cache. Based on a patch from sherbang.

In passing, changed the get() method to use a similar style to has_key() and made add() fractionally faster with the same sort of change (only compute time.time() when we really need it).

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/core/cache/backends/locmem.py

    r6822 r6904  
    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            exp = self._expire_info.get(key) 
     36            if exp is None or exp <= time.time(): 
     37                try: 
     38                    self._set(key, pickle.dumps(value), timeout) 
     39                except pickle.PickleError: 
     40                    pass 
    4941        finally: 
    5042            self._lock.writer_leaves() 
    5143 
    5244    def get(self, key, default=None): 
    53         should_delete = False 
    5445        self._lock.reader_enters() 
    5546        try: 
    56             now = time.time() 
    5747            exp = self._expire_info.get(key) 
    5848            if exp is None: 
    5949                return default 
    60             elif exp < now: 
    61                 should_delete = True 
    62             else: 
     50            elif exp > time.time(): 
    6351                try: 
    6452                    return pickle.loads(self._cache[key]) 
     
    6755        finally: 
    6856            self._lock.reader_leaves() 
    69         if should_delete: 
    70             self._lock.writer_enters() 
    71             try: 
    72                 del self._cache[key] 
    73                 del self._expire_info[key] 
    74                 return default 
    75             finally: 
    76                 self._lock.writer_leaves() 
     57        self._lock.writer_enters() 
     58        try: 
     59            del self._cache[key] 
     60            del self._expire_info[key] 
     61            return default 
     62        finally: 
     63            self._lock.writer_leaves() 
    7764 
    7865    def _set(self, key, value, timeout=None): 
     
    9683 
    9784    def has_key(self, key): 
    98         return key in self._cache 
     85        self._lock.reader_enters() 
     86        try: 
     87            exp = self._expire_info.get(key) 
     88            if exp is None: 
     89                return False 
     90            elif exp > time.time(): 
     91                return True 
     92        finally: 
     93            self._lock.reader_leaves() 
     94 
     95        self._lock.writer_enters() 
     96        try: 
     97            del self._cache[key] 
     98            del self._expire_info[key] 
     99            return False 
     100        finally: 
     101            self._lock.writer_leaves() 
    99102 
    100103    def _cull(self):