def test_expr(self): key = "expr" self.assertEquals(None, cache.get(key)) cache.set(key, u"ABC", 1) self.assertEquals(u"ABC", cache.get(key)) time.sleep(1.2) self.assertEquals(None, cache.get(key))
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 save_request_token(self, token, secret): # cache them, but first calculate expires time: now = datetime.now() t = datetime(now.year, now.month, now.day) delta = now - t exp = 86400 - delta.seconds # makes it expires in midnight (24:00) if exp > 3600: exp = 3600 logging.info('fetched and cache for %d seconds.' % exp) cache.set(token, secret, exp)
def test_cache(self): key1 = "key1" key2 = "key2" self.assertEquals(None, cache.get(key1)) self.assertEquals(None, cache.get(key2)) cache.set(key1, "abc") cache.set(key2, ["x", "y", "z"]) self.assertEquals("abc", cache.get(key1)) self.assertEquals(["x", "y", "z"], cache.get(key2)) cache.delete(key1) cache.delete(key2) self.assertEquals(None, cache.get(key1)) self.assertEquals(None, cache.get(key2))
def get_count(name): ''' Retrieve the value for a given sharded counter. Args: name: the name of the counter Returns: Integer value ''' total = cache.get(CACHE_KEY_PREFIX + name) if total is None: total = 0 for counter in ShardedCounter.all().filter('name =', name).fetch(1000): total += counter.count cache.set(CACHE_KEY_PREFIX + name, str(total)) return total return int(total)
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 _get_weather(city): if not city: return r'{"code":"500","Message":"Missing Input: city"}' data = cache.get("city-%s" % city) if data: logging.info("got data from cache") web.header("X-Cache", "Hit from cache") return data # check city: cities = list(db.select("city", where="yahoo_code=$code", vars=dict(code=city))) if not cities: return r'{"code":"500","Message":"Invalid Input: city"}' c = cities[0] w = c.yahoo_code logging.info("fetch from yahoo weather...") url = "http://weather.yahooapis.com/forecastjson?w=%s&u=c" % w resp = urllib2.urlopen(url) if resp.code == 200: logging.info("begin fetch...") data = json.loads(resp.read()) data["city"] = c.name cond = data["forecast"][0]["condition"] codes = _get_weather_code(cond) data["forecast"][0]["code"] = codes[0] data["forecast"][0]["condition"] = codes[1] data["forecast"][0]["image"] = codes[2] cond = data["forecast"][1]["condition"] codes = _get_weather_code(cond) data["forecast"][1]["code"] = codes[0] data["forecast"][1]["condition"] = codes[1] data["forecast"][1]["image"] = codes[2] # cache it, but first calculate expires time: now = datetime.now() t = datetime(now.year, now.month, now.day) delta = now - t exp = 86400 - delta.seconds # makes it expires in midnight (24:00) if exp > 3600: exp = 3600 logging.info("fetched and cache for %d seconds." % exp) json_data = json.dumps(data) cache.set("city-%s" % city, json_data, exp) web.header("X-Cache", "Miss from cache") return json_data return None