通过redis限制请求的频率
场景1: 限制用户每分钟获取验证码频率,如一分钟不能超过3次
<?php /** * 场景4:限速 * * @package scene04.php * @author ethan<toethanxu@outlook.com> * @version 2020/6/14 */ require_once '../../CacheRedis.php'; $server = '127.0.0.1:6379'; $redis = CacheRedis::getInstance($server); // 限速 $keyPrefix = 'phone:limit:'; $phone = 15111111111; $key = $keyPrefix . $phone; // 限制用户每分钟获取验证码频率,如一分钟不能超过3次 $expire = 10; // expire重复设置,会覆盖上一次过期时间 /*$total = $redis->incr($key); $redis->expire($key, $expire); var_dump($total);*/ // 不让key覆盖 /*$res = $redis->get($key); if ($res) { $total = $redis->incr($key); } else { $res = $redis->setex($key, $expire, 1); $total = 1; } if ($total <= 3) { var_dump('send success'); } else { var_dump('send limit'); }*/ // 进阶 // setnx: 若key已存在,重复设置会失败 /*$res = $redis->setnx($key, 1); if ($res) { $redis->expire($key, $expire); $total = 1; } else { $total = $redis->incr($key); } if ($total <= 3) { var_dump('send success'); } else { var_dump('send limit'); }*/ // 进阶2 $res = $redis->set($key, 1, ['ex' => $expire, 'nx']); if ($res) { $total = 1; } else { $total = $redis->incr($key); } var_dump($total); if ($total <= 3) { var_dump('send success'); } else { var_dump('send limit'); }
场景2: 限制一个ip地址不能1秒内访问超过n次(方法同场景1)
场景3:限制一个人1分钟内点赞/投票次数不能超过n(方法同场景1)
推荐相关精品课程: redis基础&实战教程