本文主要是介绍redis 1分钟内发送限频次数,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
<?php
class Send{//可以选择列表类型来实现,记录每个ip每次访问的时间,一旦列表元素超过100,就判断时间最早的元素距离现在的时间是否小于1分钟,
//如果是则表明最近一分钟超频,否则就将现在的时间加入列表同时删除最早的时间元素public $prefix = 'redis_';public function test()
{$key = $this->prefix.get_client_ip();if (Redis::llen($key)) {$count = Redis::llen($key);if ($count > 3) {$time = Redis::lrange($key, 0, 0);if (time() - $time[0] < 60) {return ['status'=>false,'msg=>'1分钟内只能发送3次'];}else{Redis::lrem($key, 1, $time[0]);}}Redis::rpush($key, time());} else {Redis::rpush($key, time());}
}}
这篇关于redis 1分钟内发送限频次数的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!