def wrapper(*args, **kwargs): from weixin.cache import data_cache key = '' for arg in args: if isinstance(arg, basestring): key = arg if not key: for arg, arg_value in kwargs.items(): if isinstance(arg_value, basestring): key = arg_value break if not key: if len(args) > 0: key = json.dumps(args[0]) elif len(kwargs) > 0: key = json.dumps(kwargs.values()[0]) else: key = uuid.uuid4().hex key += key_info key = hashlib.md5(key).hexdigest() cache_data = data_cache.get(key) if cache_data: data = json.loads(cache_data) # data = unicode2utf(data) return data data = func(*args, **kwargs) if data: data_cache.set(key, json.dumps(data, ensure_ascii=False), expire_time) return data
def wrapper(params): key = params if isinstance(params, dict): key = json.dumps(params) elif isinstance(params, list): key = json.dumps(params) elif isinstance(params, unicode): key = params.encode("utf-8") key = hashlib.md5(key).hexdigest() cache_data = data_cache.get(key) if cache_data: data = json.loads(cache_data) data = unicode2utf(data) return data data = func(params) data_cache.set(key, json.dumps(data, ensure_ascii=False), 3600) return data
def _fresh_token(self): qiniu_auth = qiniu.Auth(self.qiniu_token, self.qiniu_secret) self.auth = qiniu_auth token = qiniu_auth.upload_token(self.qiniu_bucket) data_cache.set(self.qiniu_token_key, token, 3580) return token
def cache_ip_location(ip, location, expire=86400): return data_cache.set(ip, location, expire)
def cache_lunar_date(date_str, lunar_info): return data_cache.set(date_str, lunar_info, 86400)
def cache_city_weather(city, weather, expire=1800): return data_cache.set(city, weather, expire)