def test_incr(self): key = "inc" self.assertEquals(None, cache.get(key)) cache.set(key, 123) self.assertEquals(123, cache.get(key)) cache.incr(key) self.assertEquals(124, cache.get(key)) cache.incr(key, 10) self.assertEquals(134, cache.get(key))
def is_too_many_retweets(rt): try: tid = str(rt.retweeted_status.id) except KeyError: logging.debug('-'*80 + 'KeyError' + rt.text) tid = str(rt.id) #print '----%s----%s----%s' % (rt, rt.retweeted_status.id, (tid)) times = cache.get(tid) if times: logging.info("%s: %s times %s" % (tid, times, rt.retweeted_status.text)) if times > 20: return True else: cache.incr(tid) else: cache.set(tid, '1') return False
def incr_count(name, delta=1): ''' Increment the value for a given sharded counter. Args: name: the name of the counter ''' config = ShardedCounterConfig.get_or_insert(name, name=name) def tx(): index = random.randint(0, config.shards-1) shard_name = name + str(index) counter = ShardedCounter.get_by_key_name(shard_name) if counter is None: counter = ShardedCounter(key_name=shard_name, name=name) counter.count += delta counter.put() db.run_in_transaction(tx) cache.incr(CACHE_KEY_PREFIX + name, delta=delta)