def up2cloud(filename, abs_filepath, need_rm=True, pic_host=""): ckey = "up2cloud" thekey = filename rcli = RedisClient().get_cli() thetoken = rcli.get(ckey) if thetoken: thetoken = json.loads(thetoken) else: q = Auth(access_key, secret_key) # 上传后保存的文件名 # 生成上传 Token,可以指定过期时间等 ttl = 3600 thetoken = q.upload_token(g_bucket_name, None, ttl) rcli.set(ckey, json.dumps(thetoken), ex=ttl - 600) # 要上传文件的本地路径 ret, info = put_file(thetoken, thekey, abs_filepath) uri = "" if ret and ret["key"] == thekey and ret['hash'] == etag(abs_filepath): uri = json.loads(info.text_body)["key"] if not uri: g_stdlogging.error("[up2cloud]%s %s" % (ret, info)) raise unknown_err # print(abs_filepath) if need_rm: os.remove(abs_filepath) if not pic_host: pic_host = app.config["PIC_HOST"] return pic_host + "/" + uri
def get_user_info_by_auth_redis(): auth_key = get_auth_key() if not auth_key: return None cli = RedisClient().get_cli() # res = cli.get(auth_key) res = cli.get(g_auth_key_pre + auth_key) if not res: return None return pickle.loads(res)
def wrapper(*args, **kwargs): # 包路径名+类名(非类的话,该值为参数的类型,如<class 'str'>或int)+方法名 arg_part = "" if len(args) >= 1: arg_part = str(args[0].__class__) key_pre = str(inspect.getmodule( func)) + ":" + arg_part + ":" + func.__name__ + ":" + str(ver) r_need_cache = kwargs.pop("r_need_cache", True) r_del_cache = kwargs.pop("r_del_cache", False) r_timeout = kwargs.pop("r_timeout", 0) # kwargs中的默认值,获取不到,如要获取,必须传值 tmp_dict = {} if exclude_list: for key, val in kwargs.items(): if key not in exclude_list: tmp_dict[key] = val kwargs = tmp_dict sorted_values = sorted(kwargs.items(), key=lambda val: val[0]) kwdata = urlencode(sorted_values) # 普通方法 input_key = "" for item in args: tmp = str(item.__class__) if "." in tmp: input_key += tmp + "|" else: input_key += str(item) + "|" input_key += kwdata # if not tmp_args or tmp_args[0].__class__.__module__ == "builtins": # input_key = "^^".join(tmp_args) + ":" + kwdata # # 实例或类方法 # else: # input_key = "^^".join(tmp_args[1:]) + ":" + kwdata if len(input_key) > 255: input_key = hashlib.md5(input_key.encode("utf-8")).hexdigest() key = key_pre + "|" + input_key # print(key) cli = RedisClient().get_cli() if r_del_cache: cli.delete(key) if r_need_cache: res = cli.get(key) if not res: res = func(*args, **kwargs) if r_timeout: cli.set(key, pickle.dumps(res), ex=r_timeout) else: cli.set(key, pickle.dumps(res), ex=timeout) else: res = pickle.loads(res) else: res = func(*args, **kwargs) return res
def freq_control_by_key(key, duration=600, max_times=10): cli = RedisClient().get_cli() now_times = cli.get(key) if not now_times: cli.incr(key) cli.expire(key, duration) else: now_times = int(now_times) if now_times >= max_times: return False else: cli.incr(key) return True
def freq_control_coupon_by_key(key, coupon, duration=600, max_size=10): cli = RedisClient().get_cli() coupon_size = 0 try: coupon_size = cli.scard(key) except Exception as e: logging.error("[freq_control_coupon_by_key]%s" % e) if not coupon_size: cli.sadd(key, coupon) cli.expire(key, duration) elif coupon_size >= max_size: return False else: cli.sadd(key, coupon) return True
def __init__(self, flag, ver=4, id_name="id", ex=300, need_cache=True, db=None): if db: self.db = db else: self.db = get_db() self.rcli = RedisClient().get_cli() self.flag = flag self.ver = ver self.id_name = id_name self.need_cache = need_cache self.ex = ex
def up2cloud_video_part(filename, abs_filepath, need_rm=True, pic_host=""): ckey = "up2cloud_video_part1" thekey = filename rcli = RedisClient().get_cli() thetoken = rcli.get(ckey) if False and thetoken: thetoken = json.loads(thetoken) else: q = Auth(access_key, secret_key) # 上传后保存的文件名 # 生成上传 Token,可以指定过期时间等 ttl = 3600 pat = urlsafe_base64_encode("videopart-$(count)") policy = { # "persistentOps": "vsample/jpg/ss/1/t/4/s/480x360/interval/1/pattern/dmZyYW1lLSQoY291bnQp", "persistentOps": "segment/mp4/segtime/5/pattern/%s" % pat, "persistentNotifyUrl": "https://balimiao.cn/front/qiniu/video_part_callback" } thetoken = q.upload_token(g_bucket_name, None, ttl, policy) rcli.set(ckey, json.dumps(thetoken), ex=ttl - 600) # 要上传文件的本地路径 ret, info = put_file(thetoken, thekey, abs_filepath) uri = "" if ret and ret["key"] == thekey and ret['hash'] == etag(abs_filepath): uri = json.loads(info.text_body)["key"] if not uri: g_stdlogging.error("[up2cloud_video]%s %s" % (ret, info)) raise unknown_err # print(abs_filepath) if need_rm: os.remove(abs_filepath) if not pic_host: pic_host = app.config["PIC_HOST"] return pic_host + "/" + uri