Spaces:
Runtime error
Runtime error
Download src/Service/QuotaManager.php from Alcazard/vibe-coding-api: direct link, hf CLI and curl.
- Browser
- Download file 1.65 kB
-
https://huggingface.co/spaces/Alcazard/vibe-coding-api/resolve/main/src/Service/QuotaManager.php
- Command line
-
hf download hf://spaces/Alcazard/vibe-coding-api/src/Service/QuotaManager.php
-
curl -L -o QuotaManager.php https://huggingface.co/spaces/Alcazard/vibe-coding-api/resolve/main/src/Service/QuotaManager.php
1.65 kB
| namespace src\Service; | |
| namespace VibeCoding\Service; | |
| use Symfony\Component\Cache\Adapter\FilesystemAdapter; | |
| class QuotaManager | |
| { | |
| private FilesystemAdapter $cache; | |
| private array $limits = [ | |
| 'openrouter' => 200, | |
| 'gemini' => 1500 | |
| ]; | |
| public function __construct() | |
| { | |
| $this->cache = new FilesystemAdapter(); | |
| } | |
| public function getAvailableProviders(): array | |
| { | |
| $available = []; | |
| foreach (array_keys($this->limits) as $provider) { | |
| if ($this->canUse($provider)) { | |
| $available[] = $provider; | |
| } | |
| } | |
| return $available; | |
| } | |
| public function canUse(string $provider): bool | |
| { | |
| return $this->getUsage($provider) < $this->limits[$provider]; | |
| } | |
| public function incrementUsage(string $provider): void | |
| { | |
| $key = $this->getKey($provider); | |
| $usage = $this->getUsage($provider) + 1; | |
| $item = $this->cache->getItem($key); | |
| $item->set($usage); | |
| $item->expiresAt(new \DateTime('tomorrow midnight')); | |
| $this->cache->save($item); | |
| } | |
| public function getRemaining(string $provider): int | |
| { | |
| $usage = $this->getUsage($provider); | |
| return max(0, $this->limits[$provider] - $usage); | |
| } | |
| private function getUsage(string $provider): int | |
| { | |
| $key = $this->getKey($provider); | |
| $item = $this->cache->getItem($key); | |
| return $item->isHit() ? $item->get() : 0; | |
| } | |
| private function getKey(string $provider): string | |
| { | |
| return sprintf('%s_%s', $provider, date('Y-m-d')); | |
| } | |
| } |