vendor/codeages/rate-limiter/src/RateLimiter.php

(开头部分) 1KB

这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。

<?php

namespace Codeages\RateLimiter;

use Codeages\RateLimiter\Storage\Storage;

class RateLimiter
{
    public $name;

    public $maxAllowance;

    public $period;

    private $storage;

    public function __construct($name, $maxAllowance, $period, Storage $storage)
    {
        $this->name = $name;
        $this->maxAllowance = $maxAllowance;
        $this->period = $period;
        $this->storage = $storage;
    }

    public function check($id, $use = 1.0)
    {
        $rate = $this->maxAllowance / $this->period;

        $key = $this->getKey($id);
        $value = $this->storage->get($key);
        if ($value !== false) {
            list($allowance, $lastCheckTime) = $this->unpackValue($value);

            $timePassed = time() - $lastCheckTime;
            $allowance += $timePassed * $rate;

            if ($allowance > $this->maxAllowance) {
                $allowance = $this->maxAllowance;
            }

            if ($allowance < $use) {
                $this->storage->set($key, $this->packValue($allowance, time()), $this->period);

                return 0;
            } else {
                $this->storage->set($key, $this->packValue($allowance - $use, time()), $this->period);

                return (int) floor($allowance);
            }
        } else {
            $this->storage->set($key, $this->packValue($this->maxAllowance - $use, time()), $this->period);

            return $this->maxAllowance;
        }
    }

    public function getAllow($id)
    {
        $this->check($id, 0);
        $value = $this->storage->get($this->getKey($id));

24 小时内免费解锁 3 个项目,之后 1 算力/个。 规则说明

AI 解读 AI

登录后可用,每次 10 算力,解读结果公开显示在下面。

还没有人解读过这个文件。