美文网首页
redis利用zset简单限流

redis利用zset简单限流

作者: RainingMan | 来源:发表于2019-03-16 23:54 被阅读0次

    引用出处:老钱的 Redis 深度历险:核心原理与应用实践

    # 指定用户 user_id 的某个行为 action_key 在特定的时间内 period 只允许发生一定的次数 max_count
    def is_action_allowed(user_id, action_key, period, max_count):
        return True
    # 调用这个接口 , 一分钟内只允许最多回复 5 个帖子
    can_reply = is_action_allowed("laoqian", "reply", 60, 5)
    if can_reply:
        do_reply()
    else:
        raise ActionThresholdOverflow()
    
    # coding: utf8
    
    import time
    import redis
    
    client = redis.StrictRedis()
    
    def is_action_allowed(user_id, action_key, period, max_count):
        key = 'hist:%s:%s' % (user_id, action_key)
        now_ts = int(time.time() * 1000)  # 毫秒时间戳
        with client.pipeline() as pipe:  # client 是 StrictRedis 实例
            # 记录行为
            pipe.zadd(key, now_ts, now_ts)  # value 和 score 都使用毫秒时间戳
            # 移除时间窗口之前的行为记录,剩下的都是时间窗口内的
            pipe.zremrangebyscore(key, 0, now_ts - period * 1000)
            # 获取窗口内的行为数量
            pipe.zcard(key)
            # 设置 zset 过期时间,避免冷用户持续占用内存
            # 过期时间应该等于时间窗口的长度,再多宽限 1s
            pipe.expire(key, period + 1)
            # 批量执行
            _, _, current_count, _ = pipe.execute()
        # 比较数量是否超标
        return current_count <= max_count
    
    
    for i in range(20):
        print is_action_allowed("laoqian", "reply", 60, 5)
    

    相关文章

      网友评论

          本文标题:redis利用zset简单限流

          本文链接:https://www.haomeiwen.com/subject/vtxymqtx.html