- 按 docs/renew-code-comment.md 将 PHP 文件头改为带边框的 Author 注释\n- 注释中的 crmeb.com 替换为 uj345.cn(代码字符串中的外链未改)\n- 新增 docs/renew-code-comment.md 说明 Made-with: Cursor
59 lines
1.3 KiB
PHP
59 lines
1.3 KiB
PHP
<?php
|
|
// +----------------------------------------------------------------------
|
|
// | Author: ScottPan Team
|
|
// +----------------------------------------------------------------------
|
|
|
|
namespace app\http\middleware\api;
|
|
|
|
|
|
use app\Request;
|
|
use crmeb\exceptions\ApiException;
|
|
use crmeb\interfaces\MiddlewareInterface;
|
|
use crmeb\services\CacheService;
|
|
|
|
/**
|
|
* reids锁
|
|
* Class BlockerMiddleware
|
|
* @author 等风来
|
|
* @email 136327134@qq.com
|
|
* @date 2022/11/21
|
|
* @package app\http\middleware\api
|
|
*/
|
|
class BlockerMiddleware implements MiddlewareInterface
|
|
{
|
|
|
|
/**
|
|
* @param Request $request
|
|
* @param \Closure $next
|
|
* @author 等风来
|
|
* @email 136327134@qq.com
|
|
* @date 2022/11/21
|
|
*/
|
|
public function handle(Request $request, \Closure $next)
|
|
{
|
|
$uid = $request->uid();
|
|
$key = md5($request->rule()->getRule() . $uid. json_encode($request->param()));
|
|
if (!CacheService::setMutex($key)) {
|
|
throw new ApiException('请求太过频繁,请稍后再试');
|
|
}
|
|
|
|
$response = $next($request);
|
|
|
|
$this->after($response, $key);
|
|
|
|
return $response;
|
|
}
|
|
|
|
/**
|
|
* @param $response
|
|
* @param $key
|
|
* @author 等风来
|
|
* @email 136327134@qq.com
|
|
* @date 2022/11/22
|
|
*/
|
|
public function after($response, $key)
|
|
{
|
|
CacheService::delMutex($key);
|
|
}
|
|
}
|