def _wrapper(*args, **kwargs): cached_key = _get_cached_key(func, args, kwargs, prefix, key) data = get_redis_client().get(cached_key) if data: _logger.debug("get cached result by key '%s'" % cached_key) try: result = json2dict(data) except Exception: _logger.warn("Fail to parse cached result to json: %s. Call function %s directly." % (data, func.__name__), exc_info=True) result = yield func(*args, **kwargs) raise Return(result) else: result = yield func(*args, **kwargs) if result is not None: if isinstance(expires_in, basestring): cached_expires_in = eval("_config.%s" % expires_in) else: cached_expires_in = expires_in _logger.debug("cache result by key '%s' and expired period %d" % (cached_key, cached_expires_in)) get_redis_client().setex(cached_key, cached_expires_in, dict2json(result)) raise Return(result)
def _wrapper(*args, **kwargs): cached_key = _get_cached_key(func, args, kwargs, prefix, key) _logger.debug("delete cached result by key '%s'" % cached_key) get_redis_client().delete(cached_key) result = yield func(*args, **kwargs) raise Return(result)
def get_cache(prefix, user_id, field): cached_key = _get_key(prefix, user_id, field) data = get_redis_client().get(cached_key) result = None if data: _logger.debug("get cached result by key '%s'" % cached_key) try: result = json2dict(data) except Exception as e: _logger.warn("Fail to parse cached result to json: %s." % (data), exc_info=True) raise e raise Return(result)
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)
def get_keys(pattern=""): return get_redis_client().keys(pattern)