首页 技术 正文
技术 2022年11月15日
0 收藏 316 点赞 4,237 浏览 2125 个字

https://cloud.tencent.com/developer/article/1347437   python中的Redis键空间通知(过期回调)

set notify-keyspace-events KEA  【KEA参照以下字符进行设置】
此有缺点:最大的缺点是Pub / Sub实现要求发布者和订阅者一直处于启动状态。订阅服务器在停止或连接丢失时会丢失数据。
  【意思:就是如果服务端在意外情况下出现重启或断开,需要重新设置(windows)】
字符 发送通知
K 键空间通知,所有通知以 keyspace@ 为前缀,针对Key
E 键事件通知,所有通知以 keyevent@ 为前缀,针对event
g DEL 、 EXPIRE 、 RENAME 等类型无关的通用命令的通知
$ 字符串命令的通知
l 列表命令的通知
s 集合命令的通知
h 哈希命令的通知
z 有序集合命令的通知
x 过期事件:每当有过期键被删除时发送
e 驱逐(evict)事件:每当有键因为 maxmemory 政策而被删除时发送
A 参数 g$lshzxe 的别名,相当于是All
import time
from redis import StrictRedisredis = StrictRedis(host='localhost', port=6379)pubsub = redis.pubsub()def event_handler(msg):
print(msg)
data = msg['channel'].decode().split(':')[1]
print('***',data, redis.get(data))pubsub.psubscribe(**{'__keyspace@0__:*': event_handler})print('Starting message loop')
while True:
message = pubsub.get_message()
if message:
print(message)
else:
time.sleep(0.01)

—订阅端

import time
from redis import StrictRedisredis = StrictRedis(host='localhost', port=6379)pubsub = redis.pubsub()
pubsub.psubscribe('__keyspace@0__:*')print('Starting message loop')
while True:
message = pubsub.get_message()
if message:
print(message)
else:
time.sleep(0.01)

–订阅端若不添加回调事件

# python 3.7
import redis
pool = redis.ConnectionPool(host='localhost', port=6379)
r = redis.Redis(connection_pool=pool)
r.execute_command('config set notify-keyspace-events KEA') # 发布端,判断如果是第一次就执行
r.setex('a2',2,'a1')

–发布端


import redis,json,time
r = redis.ConnectionPool(host='127.0.0.1',port=6379)
rw = redis.Redis(connection_pool=r)
for i in range(1,10):
rw.zadd('cookie_pool',json.dumps({'a1':1,'a2':2}),int(time.time()))
time.sleep(1)
print('设置 1个')def alive(time_space=10 * 3):
'''
:param time: 每隔10分钟检测一下
:return:
'''
# 对应 _self.rw.zadd('cookie_pool', json.dumps(d), int(time.time()))
while True:
# 拿到前面的1个,并loads
_ = rw.zrange('cookie_pool', 0, 0)[0] # 拿到分值
score = rw.zscore('cookie_pool', _) print(score,time.time()-score)
# 如果分值大于10分钟,就开始进行验证cook保活
if time.time() - score >= time_space:
# 进行删除
rw.zrem('cookie_pool', _) # 转义第一个集合的值
first_set = json.loads(_.decode()) # 此处调用cook保活验证,,返回 bool,,假设为True
cook_alive = True
if cook_alive:
print('正在设置',first_set)
rw.zadd('cookie_pool', json.dumps(first_set), int(time.time()))
else:
print('已自动删除')
# 什么都不做,自动扔掉
else:
print('检测时间未到')
time.sleep(1)
alive()

—-有序集合---用分值判断间隔时间用作不间段保活

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,910
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,435
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,250
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,061
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,693
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,731