我们从Python开源项目中,提取了以下9个代码示例,用于说明如何使用django.core.cache.cache.incr()。
def rejudge_submission_set(submission_set): cache.set(REJUDGE_COUNTER, 0) def decrease_counter(): try: cache.decr(REJUDGE_COUNTER, 1) except ValueError: cache.set(REJUDGE_COUNTER, 0) with transaction.atomic(): for submission in submission_set: submission.status_private = submission.status = SubmissionStatus.WAITING submission.status_detail = "" submission.status_message = "" submission.save(update_fields=["status_private", "status", "status_detail", "status_message"]) for submission in submission_set: while cache.get_or_set(REJUDGE_COUNTER, 0) >= REJUDGE_TASK_LIMIT: sleep(1) try: cache.incr(REJUDGE_COUNTER, 1) except: cache.set(REJUDGE_COUNTER, 0) rejudge_submission(submission, decrease_counter)
def test_incr_decr_timeout(self): """incr/decr does not modify expiry time (matches memcached behavior)""" cache = self.cache key = 'value' cache.set(key, 1, timeout=10000) _, exp = cache._get(key) cache.incr(key) self.assertEqual(exp, cache._get(key)[1]) cache.decr(key) self.assertEqual(exp, cache._get(key)[1])
def test_incr(self): # Cache values can be incremented cache = self.cache cache.set('answer', 41) self.assertEqual(cache.incr('answer'), 42) self.assertEqual(cache.get('answer'), 42) self.assertEqual(cache.incr('answer', 10), 52) self.assertEqual(cache.get('answer'), 52) self.assertEqual(cache.incr('answer', -10), 42) with self.assertRaises(ValueError): cache.incr('does_not_exist')
def test_cache_versioning_incr_decr(self): cache = self.cache cache2 = LRUObjectCache('lru2', dict(VERSION=2)) cache2._cache = cache._cache cache.set('answer1', 37, version=1) cache.set('answer1', 42, version=2) cache.incr('answer1') self.assertEqual(cache.get('answer1', version=1), 38) self.assertEqual(cache.get('answer1', version=2), 42) cache.decr('answer1') self.assertEqual(cache.get('answer1', version=1), 37) self.assertEqual(cache.get('answer1', version=2), 42) cache.set('answer2', 37, version=1) cache.set('answer2', 42, version=2) cache.incr('answer2', version=2) self.assertEqual(cache.get('answer2', version=1), 37) self.assertEqual(cache.get('answer2', version=2), 43) cache.decr('answer2', version=2) self.assertEqual(cache.get('answer2', version=1), 37) self.assertEqual(cache.get('answer2', version=2), 42) cache.set('answer3', 37, version=1) cache.set('answer3', 42, version=2) cache2.incr('answer3') self.assertEqual(cache.get('answer3', version=1), 37) self.assertEqual(cache.get('answer3', version=2), 43) cache2.decr('answer3') self.assertEqual(cache.get('answer3', version=1), 37) self.assertEqual(cache.get('answer3', version=2), 42) cache.set('answer4', 37, version=1) cache.set('answer4', 42, version=2) cache2.incr('answer4', version=1) self.assertEqual(cache.get('answer4', version=1), 38) self.assertEqual(cache.get('answer4', version=2), 42) cache2.decr('answer4', version=1) self.assertEqual(cache.get('answer4', version=1), 37) self.assertEqual(cache.get('answer4', version=2), 42)
def cache_incr(self, key): # memcache is only backend that can increment atomically try: # add first, to ensure the key exists cache.add(key, 0, timeout=self.expire_after()) cache.incr(key) except AttributeError: cache.set(key, cache.get(key, 0) + 1, self.expire_after())
def _increase(k): cache.incr(k)
def clear_permission_cache(): from django.core.cache import cache version = get_cache_permission_version() if version > 1: cache.incr(get_cache_permission_version_key()) else: cache.set(get_cache_permission_version_key(), 2, get_cms_setting('CACHE_DURATIONS')['permissions'])
def test_incr(self): key = 'key-to-incr' await cache.set(key, 1) await cache.incr(key) result = await cache.get(key) self.assertEqual(result, 2)
def test_incr(self): key = "album_total_num" cache.set(key, self.num_albums) result = cache.incr(key, 1) self.assertEqual(result, self.num_albums+1)