def set_cache(prefix, user_id, field, data={}, cached_expires_in = 3600):
    cached_key = _get_key(prefix, user_id, field)
    
    if data is not None:
        if isinstance(data, dict):
            lat = get_timestamp()
            data['lat'] = lat
        if cached_expires_in == 0:
            get_redis_client().set(cached_key, dict2json(data))
        else :
            get_redis_client().setex(cached_key, cached_expires_in, dict2json(data))
        _logger.debug("cache result by key '%s' and expired period %d" % (cached_key, cached_expires_in))
    else :
        _logger.debug("delete cached result by key '%s'" % cached_key)
        get_redis_client().delete(cached_key)
Exemple #2
0
 def task_isr(self):        
     # Query all the authorized user of fitbit service.
     users = yield get_keys("%s:auth:*" % CACHED_PREFIX_KEY)
     for user in users:
         suid = user.split(":")[-1]
         # TODO: check suid format.
         auth_info = yield get_user_auth(suid, prefix=CACHED_PREFIX_KEY)
         if auth_info is None:
             raise FlamesError(USER_NOT_FOUND, user_id=suid)
         curr_time = get_timestamp()
         lat = auth_info.get('lat', 0)
         expr_time = auth_info.get('expires_in', 3600)
         # Check the auth token status.
         if curr_time - lat >= expr_time:
             # Token need be updated.
             auth_info = yield self.api_clnt.refresh_token(suid)
         for act in self.api_clnt._actions:
             task_info ={'auth': auth_info, 'suid': suid, 'action': act}
             kafka_queue_send(self.producer, dict2json(task_info), partition_key=act)
     raise Return(None)