• 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
    g DEL 、 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----有序集合---用分值判断间隔时间用作不间段保活
  • 相关阅读:
    php 计算概率,可以任意添加
    如何绕过浏览器的弹窗拦截机制
    javascript iframe 操作(一)
    视频学习站
    技术博文
    js如何打印对象
    云主机
    cookie小细节
    cookie细节
    实用网址
  • 原文地址:https://www.cnblogs.com/Skyda/p/10281915.html
Copyright © 2020-2023  润新知