验证中...
php中利用redis限制接口请求频率
Raw Copy
<?php
function getRedis()
{
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
return $redis;
}
function getApiLimit($api, $default = '2000')
{
//接口名称 = 每次请求ms间隔
$info = [
'user.info' => '500',
'user.login' => '1000'
];
return isset($info[$api]) ? $info[$api] : $default;
}
$api = isset($_GET['path']) ? $_GET['path'] : 'main';
$time = getApiLimit($api);
$key = "api_limit_" . $api;
$redis = getRedis();
$num = $redis->incr($key);
if($num == 1) {
$redis->pExpire($key, $time);
} else {
exit('api limit');
}
echo 'success';

Comment list( 0 )

You need to Sign in for post a comment

Help Search