mysql接口访问频率限制_如何利用 Redis 实现接口频次限制_Redis_数据库

介绍:

我们可以利用 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。

到此这篇关于如何利用 Redis 实现接口频次限制的文章就介绍到这了,更多相关Redis 实现接口频次限制内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Django1.8 中,我们可以通过使用 Redis实现接口访问频率限制。具体实现步骤如下: 1. 首先,在 settings.py 文件中配置 Redis: ``` CACHES = { 'default': { 'BACKEND': 'django_redis.cache.RedisCache', 'LOCATION': 'redis://127.0.0.1:6379/1', 'OPTIONS': { 'CLIENT_CLASS': 'django_redis.client.DefaultClient', } } } ``` 2. 在 views.py 文件中定义装饰器: ``` from django.core.cache import cache from django.http import HttpResponseBadRequest def rate_limit(key_prefix, rate, block=False): def decorator(view_func): def wrapper(request, *args, **kwargs): # 计算 key key = '{0}:{1}'.format(key_prefix, request.META['REMOTE_ADDR']) # 获取当前时间 now = int(time.time()) # 获取 key 对应的数据 data = cache.get(key) if not data: # 如果 key 对应的数据不存在,说明还没有访问过,直接存储当前时间 cache.set(key, '{0}:1'.format(now), rate) else: # 否则,将数据分解成时间戳和计数器 timestamp, count = data.split(':') # 如果当前时间戳减去存储的时间戳大于限制时间,则重置计数器和时间戳 if now - int(timestamp) > rate: cache.set(key, '{0}:1'.format(now), rate) else: # 否则,将计数器加1 count = int(count) + 1 cache.set(key, '{0}:{1}'.format(timestamp, count), rate) # 如果计数器大于限制次数,则返回错误信息 if count > rate: if block: return HttpResponseBadRequest('Rate limit exceed.') else: return HttpResponseBadRequest('Rate limit exceed. Retry after {0} seconds.'.format(int(timestamp) + rate - now)) return view_func(request, *args, **kwargs) return wrapper return decorator ``` 3. 在需要进行频率限制的视图函数上使用装饰器: ``` @rate_limit('api', 10, block=True) def my_view(request): # 处理请求 pass ``` 其中,`'api'` 是 key 的前缀,`10` 是限制时间(秒),`block=True` 示超过限制次数时阻塞请求,否则返回错误信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值