python redis連線 有序集合去重
阿新 • • 發佈:2019-08-04
# -*- 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