You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
pdd-order-api/app/libs/tool/class.RequestLimitTool.php

32 lines
866 B
PHP

<?php
class RequestLimitTool {
public static function checkIsLimitByTimeMap($timeMapkey, $limitTime, $limitCount) {
$statCache = RedisExt::factory('statCache');
$data = $statCache->hGetAll($timeMapkey);
$deleteKey = [];
$total = 0;
$now = time();
foreach ($data as $time => $count) {
if ($time < $now - $limitTime) {
$deleteKey[] = $time;
} else {
$total += $count;
}
}
if ($deleteKey) {
$statCache->hDel($timeMapkey, ...$deleteKey);
}
if ($total > $limitCount) {
return [true, $total];
}
$statCache->hIncrBy($timeMapkey, $now, 1);
$expireAt = $now + $limitTime + 10;
$statCache->expireAt($timeMapkey, $expireAt);
return [false, $total];
}
}