RedisRateLimitStorage
in package
implements
IRateLimitStorage
Redis-based rate limit storage for production use.
Uses Redis sorted sets with sliding window algorithm for accurate rate limiting across distributed systems.
Table of Contents
Interfaces
- IRateLimitStorage
- Interface for rate limit storage implementations.
Properties
Methods
- __construct() : mixed
- __destruct() : mixed
- Close Redis connection.
- allow() : bool
- Check if a request is allowed under the rate limit.
- clear() : void
- Clear all rate limit data (useful for testing).
- getRemainingAttempts() : int
- Get the number of remaining attempts for a key.
- getResetTime() : int
- Get the timestamp when the rate limit window resets.
- reset() : void
- Reset the rate limit counter for a specific key.
- getRedis() : Redis
- Get Redis connection.
Properties
$_config
private
array<string|int, mixed>
$_config
$_prefix
private
string
$_prefix
$_redis
private
Redis|null
$_redis
= null
Methods
__construct()
public
__construct([array<string|int, mixed> $config = [] ]) : mixed
Parameters
- $config : array<string|int, mixed> = []
-
Redis configuration
__destruct()
Close Redis connection.
public
__destruct() : mixed
allow()
Check if a request is allowed under the rate limit.
public
allow(string $key, int $limit, int $window) : bool
Parameters
- $key : string
-
The unique identifier for the rate limit (e.g., IP address, user ID)
- $limit : int
-
Maximum number of requests allowed in the window
- $window : int
-
Time window in seconds
Tags
Return values
bool —True if request is allowed, false if rate limit exceeded
clear()
Clear all rate limit data (useful for testing).
public
clear() : void
Tags
getRemainingAttempts()
Get the number of remaining attempts for a key.
public
getRemainingAttempts(string $key, int $limit, int $window) : int
Parameters
- $key : string
-
The unique identifier for the rate limit
- $limit : int
-
Maximum number of requests allowed
- $window : int
-
Time window in seconds
Tags
Return values
int —Number of remaining attempts
getResetTime()
Get the timestamp when the rate limit window resets.
public
getResetTime(string $key, int $window) : int
Parameters
- $key : string
-
The unique identifier for the rate limit
- $window : int
-
Time window in seconds
Tags
Return values
int —Unix timestamp of reset time
reset()
Reset the rate limit counter for a specific key.
public
reset(string $key) : void
Parameters
- $key : string
-
The unique identifier to reset
Tags
getRedis()
Get Redis connection.
private
getRedis() : Redis