![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/mautic.corals.io/vendor/noxlogic/ratelimit-bundle/Service/Storage/ |
<?php namespace Noxlogic\RateLimitBundle\Service\Storage; use Noxlogic\RateLimitBundle\Service\RateLimitInfo; use Noxlogic\RateLimitBundle\Service\Storage\StorageInterface; use Psr\Cache\CacheItemPoolInterface; class PsrCache implements StorageInterface { /** * @var CacheItemPoolInterface */ protected $client; public function __construct(CacheItemPoolInterface $client) { $this->client = $client; } public function getRateInfo($key) { $item = $this->client->getItem($key); if (!$item->isHit()) { return false; } return $this->createRateInfo($item->get()); } public function limitRate($key) { $item = $this->client->getItem($key); if (!$item->isHit()) { return false; } $info = $item->get(); $info['calls']++; $item->set($info); $item->expiresAfter($info['reset'] - time()); $this->client->save($item); return $this->createRateInfo($info); } public function createRate($key, $limit, $period) { $info = [ 'limit' => $limit, 'calls' => 1, 'reset' => time() + $period, ]; $item = $this->client->getItem($key); $item->set($info); $item->expiresAfter($period); $this->client->save($item); return $this->createRateInfo($info); } public function resetRate($key) { $this->client->deleteItem($key); return true; } private function createRateInfo(array $info) { $rateLimitInfo = new RateLimitInfo(); $rateLimitInfo->setLimit($info['limit']); $rateLimitInfo->setCalls($info['calls']); $rateLimitInfo->setResetTimestamp($info['reset']); return $rateLimitInfo; } }