Files

21 lines
595 B
PHP
Raw Permalink Normal View History

<?php
declare( strict_types=1 );
namespace StreamGoals\PayPal;
/** Fixed-window counter. Best-effort (non-atomic) which is fine for abuse throttling. */
final class RateLimiter {
public function __construct( private Cache $cache ) {}
/** Returns true if the call is allowed, false if the limit is exceeded. */
public function allow( string $key, int $limit, int $windowSeconds ): bool {
$k = 'sg_rl_' . md5( $key );
$count = (int) $this->cache->get( $k );
if ( $count >= $limit ) {
return false;
}
$this->cache->set( $k, $count + 1, $windowSeconds );
return true;
}
}