Ticket #16858: locmem_incr_expiration_16858.diff

File locmem_incr_expiration_16858.diff, 2.2 KB (added by Michael Manfre, 13 years ago)

locmem cache's incr() will no longer change timeout to mimic memcached

  • django/core/cache/backends/locmem.py

    diff --git a/django/core/cache/backends/locmem.py b/django/core/cache/backends/locmem.py
    a b  
    1515_expire_info = {}
    1616_locks = {}
    1717
     18# Special timeout value used by incr and decr to instruct the cache to
     19# not change the existing timeout value
     20IGNORE_TIMEOUT = 'ignore'
     21
    1822class LocMemCache(BaseCache):
    1923    def __init__(self, name, params):
    2024        BaseCache.__init__(self, params)
     
    7175        if timeout is None:
    7276            timeout = self.default_timeout
    7377        self._cache[key] = value
    74         self._expire_info[key] = time.time() + timeout
     78        if timeout != IGNORE_TIMEOUT:
     79            self._expire_info[key] = time.time() + timeout
    7580
    7681    def set(self, key, value, timeout=None, version=None):
    7782        key = self.make_key(key, version=version)
     
    8489        finally:
    8590            self._lock.writer_leaves()
    8691
     92    def incr(self, key, delta=1, version=None):
     93        """
     94        Add delta to value in the cache. If the key does not exist, raise a
     95        ValueError exception.
     96        """
     97        value = self.get(key, version=version)
     98        if value is None:
     99            raise ValueError("Key '%s' not found" % key)
     100        new_value = value + delta
     101        self.set(key, new_value, version=version, timeout=IGNORE_TIMEOUT)
     102        return new_value
     103
    87104    def has_key(self, key, version=None):
    88105        key = self.make_key(key, version=version)
    89106        self.validate_key(key)
  • tests/regressiontests/cache/tests.py

    diff --git a/tests/regressiontests/cache/tests.py b/tests/regressiontests/cache/tests.py
    a b  
    822822        self.assertEqual(mirror_cache.get('value1'), 42)
    823823        self.assertEqual(other_cache.get('value1'), None)
    824824
     825    def test_incr_expiration(self):
     826        self.cache.set('val', 42, timeout=2)
     827        full_key = self.cache.make_key('val')
     828        exp = self.cache._expire_info[full_key]
     829        self.cache.incr('val')
     830        exp2 = self.cache._expire_info[full_key]
     831        self.assertEqual(exp, exp2)
     832               
    825833
    826834# memcached backend isn't guaranteed to be available.
    827835# To check the memcached backend, the test settings file will
Back to Top