PHP+REDIS设置请求限流(设置1秒内最大请求数1000QPS)

复制代码
双十一期间要做活动,设置商品请求限流,护航秒杀活动正常进行!

#设置1秒内最多同时1000请求
$maxNum = 1000;
$redisKey = 'GoldMall:Huodong:gid'.$g_id.'_'.date('s');
$onlineNum = (int)$this->redis->get($redisKey);
if($onlineNum){
    $onlineNum++;
    $this->redis->set($redisKey,$onlineNum,1);
}else{
    $onlineNum = 1;
    $this->redis->set($redisKey,$onlineNum,1);
}
if($onlineNum>$maxNum){
    $this->jsonResponse(401,"服务器繁忙请稍后再试~{$onlineNum}");
}

以上代码经过实践测试,OK的!

相关推荐
SelectDB14 小时前
Litefuse 开源并推出单进程轻量模式,25 秒就能跑起来的 Agent 可观测与评估平台
运维·后端·自动化运维
两个人的幸福2 天前
Windows 桌面应用自研 PHP 队列(下):完整代码与六大工程化优化
php
XIAOHEZIcode2 天前
Linux系统鼠标偏移常见原因以及修复方案
linux·运维·游戏
用户0328472220703 天前
如何搭建本地yum源(上)
运维
用户3169353811834 天前
Java连接Redis
redis
BingoGo4 天前
PHP 泛型之殇 泛型 RFC 提案被拒绝
后端·php
JaguarJack4 天前
PHP 泛型之殇 泛型 RFC 提案被拒绝
后端·php
用户3074596982075 天前
PHP 扩展——从入门到理解
php
鹏仔先生5 天前
拷贝漫画APP下载页PHP程序,后台带免费AI写作
php
大树886 天前
金刚石散热越强,管路越先见顶
大数据·运维·服务器·人工智能·ai