Ticket #17287: patch.diff

File patch.diff, 1.9 KB (added by Ivan Virabyan, 12 years ago)
  • django/core/cache/backends/locmem.py

     
    8787        finally:
    8888            self._lock.writer_leaves()
    8989
     90    def incr(self, key, delta=1, version=None):
     91        value = self.get(key, version=version)
     92        if value is None:
     93            raise ValueError("Key '%s' not found" % key)
     94        new_value = value + delta
     95        key = self.make_key(key, version=version)
     96        self._lock.writer_enters()
     97        try:
     98            pickled = pickle.dumps(new_value, pickle.HIGHEST_PROTOCOL)
     99            self._cache[key] = pickled
     100        except pickle.PickleError:
     101            pass
     102        finally:
     103            self._lock.writer_leaves()
     104        return new_value
     105
    90106    def has_key(self, key, version=None):
    91107        key = self.make_key(key, version=version)
    92108        self.validate_key(key)
  • tests/regressiontests/cache/tests.py

     
    865865        self.assertEqual(mirror_cache.get('value1'), 42)
    866866        self.assertEqual(other_cache.get('value1'), None)
    867867
     868    def test_incr_decr_timeout(self):
     869        key = 'value'
     870        _key = self.cache.make_key(key)
     871        self.cache.set(key, 1, timeout=self.cache.default_timeout*10)
     872        expire = self.cache._expire_info[_key]
     873        self.cache.incr(key)
     874        self.assertEqual(expire, self.cache._expire_info[_key])
     875        self.cache.decr(key)
     876        self.assertEqual(expire, self.cache._expire_info[_key])
    868877
    869878# memcached backend isn't guaranteed to be available.
    870879# To check the memcached backend, the test settings file will
Back to Top