redis过期事件回调函数,与有序集合

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

set notify-keyspace-events KEA  【KEA参照以下字符进行设置】
此有缺点:最大的缺点是Pub / Sub实现要求发布者和订阅者一直处于启动状态。订阅服务器在停止或连接丢失时会丢失数据。
  【意思:就是如果服务端在意外情况下出现重启或断开,需要重新设置(windows)】
字符发送通知
K键空间通知,所有通知以 keyspace@ 为前缀,针对Key
E键事件通知,所有通知以 keyevent@ 为前缀,针对event
gDEL 、 EXPIRE 、 RENAME 等类型无关的通用命令的通知
$字符串命令的通知
l列表命令的通知
s集合命令的通知
h哈希命令的通知
z有序集合命令的通知
x过期事件:每当有过期键被删除时发送
e驱逐(evict)事件:每当有键因为 maxmemory 政策而被删除时发送
A参数 g$lshzxe 的别名,相当于是All
import time  
from redis import StrictRedis

redis = 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)
View Code---订阅端
import time  
from redis import StrictRedis

redis = 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)
View Code--订阅端若不添加回调事件

 

# 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')
View Code--发布端

 

 


 

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()
View Code----有序集合---用分值判断间隔时间用作不间段保活

 

转载于:https://www.cnblogs.com/Skyda/p/10281915.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值