mirror of
https://git.sr.ht/~iwakuralain/text0Nly
synced 2025-07-27 15:36:11 +00:00
32 lines
693 B
PHP
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();
|
|
}
|
|
}
|