text0Nly/main/RateLimiter.php
Lain Iwakura 6cc76cf728
rediska
2025-06-16 01:59:41 +03:00

32 lines
693 B
PHP

<?php
class RateLimiter {
private $redis;
private $maxRequests = 5;
private $timeWindow = 3;
public function __construct() {
$this->redis = new Redis();
$this->redis->connect('127.0.0.1', 6379);
}
public function isAllowed($ip) {
$key = "rate_limit:{$ip}";
$current = $this->redis->get($key);
if (!$current) {
$this->redis->setex($key, $this->timeWindow, 1);
return true;
}
if ($current >= $this->maxRequests) {
return false;
}
$this->redis->incr($key);
return true;
}
public function __destruct() {
$this->redis->close();
}
}