# -*- coding: utf-8 -*-
import redis
from constant import redis_ip, redis_db, redis_pw, logger, redis_zset_clean_date, redis_zset_name, \
move_file_lm
pool = redis.ConnectionPool(host=redis_ip, db=redis_db, password=redis_pw)
# pool = redis.ConnectionPool(db=6, password=redis_pw)
rr = redis.Redis(connection_pool=pool) # # 客户端StrictRedis=Redis 合并了
pipe = rr.pipeline(transaction=True) # 批量操作, 遇到异步容易出错, 可以不使用, 直接用 rr 连接对象,一次一个添加
def distinct_md5(date, eml_md5, eml_path, redis_fail_path):
#
# pipe.zadd(redis_zset_name, eml_md5, date)
# pipe.zadd(redis_zset_name, {date: eml_md5}, nx=True) # nx 只插入,存在的值不会修改其分数
# pipe.zadd(redis_zset_name, {date: eml_md5})
pipe.zadd(redis_zset_name, {eml_md5: date})
return pipe.execute()[0] # 1 添加, 0 没添加
# pipe.zcard('eml_md5_zset') # 查看总个数
def del_redis_md5(n, m): # 按照分数值进行删除
try:
pipe.zremrangebyscore(redis_zset_name, n, m)
except Exception as e:
logger.warning("删除Redis缓存中md5值失败: {},".format(e))
return
pipe.execute()
# return
# return pipe.execute()[0]
def clean_redis_zset(md5_score):
if md5_score <= redis_zset_clean_date:
md5_score += 31
md5_clean_score = md5_score - redis_zset_clean_date
del_redis_md5(md5_clean_score - 0.5, md5_clean_score + 0.5) # 清理Redis上的md5 闭区间
# print(distinct_md5(redis_zset_name, "qwe", 2, "sdfsd", "sdfsdf"))
#print(distinct_md5(27, "qweqwe","sdfsd", "sdfsdf"))
# print(del_redis_md5(redis_zset_name, 1, 10))
posted on 2019-08-04 14:49 宋喜阳 阅读(...) 评论(...) 编辑 收藏