DDR爱好者之家 Design By 杰米

介绍:

我们可以利用 redis 过期Key来实现接口的频次限制。可以自定义一些访问的(速度)限制条件来把那些触发限制的请求拒之门外.一般常用来进行对爬虫的限制.

下面就利用 redis 来实现了一个简单的案例:

装饰器实现

def frequency_limit(f):
  @wraps(f)
  def frequency_function(*args, **kwargs):
    if 'csrf_token' in session:
      token = session.get("csrf_token")
      url_ = request.url_rule
      redis_key = token + str(url_)
      conn = redis.StrictRedis(host="127.0.0.1", port="6379", password="123456", db=0)
      clicks = conn.get(redis_key)
      if not clicks:
        conn.set(redis_key, 1)
        conn.expire(redis_key, 60)
      else:
        if int(clicks) >= 5:
          return jsonify({'code': 500, 'status': 0, 'message': "您的访问频率太快,请稍后再试", 'data': [],
                  'token': token})
        overdue = 1 if conn.ttl(redis_key) <= 0 else conn.ttl(redis_key)
        conn.set(redis_key, int(clicks) + 1)
        conn.expire(redis_key, overdue)
    return f(*args, **kwargs)

  return frequency_function

注:在使用 redis Key过期的时候需要注意,在设置了过期时间后,再次改变 Key 的 Value 值时,之前设置的过期时间会失效。

解决办法:

1)在修改 Value 值的时候,查一下过期时间还有多少 ttl 在修改值的时候把过期时间重新赋值回去(本文用的就是此方法)

2)redis 中设置了过期时间,如果 list 结构中添加一个数据或者改变 hset 数据的一个字段是不会清除超时时间的;

官方网站看了一下expire的说明:
这样解释的:

The timeout will only be cleared by commands that delete or overwrite the contents of the key, including DEL, SET, GETSET and all the *STORE commands. This means that all the operations that conceptually alter the value stored at the key without replacing it with a new one will leave the timeout untouched. For instance, incrementing the value of a key with INCR, pushing a new value into a list with LPUSH, or altering the field value of a hash with HSET are all operations that will leave the timeout untouched.

如果用DEL, SET, GETSET会将key对应存储的值替换成新的,命令也会清除掉超时时间;如果list结构中添加一个数据或者改变hset数据的一个字段是不会清除超时时间的;如果想要通过set去覆盖值那就必须重新设置expire。

DDR爱好者之家 Design By 杰米
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
DDR爱好者之家 Design By 杰米

P70系列延期,华为新旗舰将在下月发布

3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。

而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?

根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。