the browser-facing portion of osu!
at master 1.0 kB view raw
1<?php 2 3// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the GNU Affero General Public License v3.0. 4// See the LICENCE file in the repository root for full licence text. 5 6declare(strict_types=1); 7 8namespace App\Libraries; 9 10use Illuminate\Cache\RateLimiter as CacheRateLimiter; 11 12class RateLimiter extends CacheRateLimiter 13{ 14 // overriden version of hit() to support a cost. 15 public function hit($key, $decaySeconds = 60, int $cost = 1) 16 { 17 $key = $this->cleanRateLimiterKey($key); 18 19 $this->cache->add( 20 $key.':timer', 21 $this->availableAt($decaySeconds), 22 $decaySeconds 23 ); 24 25 // TODO: this can be better 26 $added = $this->cache->add($key, 0, $decaySeconds); 27 28 $hits = (int) $this->cache->increment($key, $cost); 29 30 // supposedly to make sure any existing key has an expiry. 31 if (!$added && $hits === $cost) { 32 $this->cache->put($key, $cost, $decaySeconds); 33 } 34 35 return $hits; 36 } 37}