FileRateLimitStorage
in package
implements
IRateLimitStorage
File-based rate limit storage for simple deployments.
Uses file locking for concurrency control. Suitable for single-server deployments where Redis is not available.
Table of Contents
Interfaces
- IRateLimitStorage
- Interface for rate limit storage implementations.
Properties
- $_gcProbability : float
- $_path : string
- $_prefix : string
- $clock : IClock
Methods
- __construct() : mixed
- allow() : bool
- Check if a request is allowed under the rate limit.
- clear() : void
- Clear all rate limit data (useful for testing).
- gc() : int
- Garbage collection - remove expired files.
- 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.
- getFilePath() : string
- Get file path for a key.
- readFile() : array<string|int, mixed>|null
- Read data from file with locking.
- writeFile() : bool
- Write data to file with locking.
Properties
$_gcProbability
private
float
$_gcProbability
$_path
private
string
$_path
$_prefix
private
string
$_prefix
$clock
private
IClock
$clock
Methods
__construct()
public
__construct([array<string|int, mixed> $config = [] ][, IClock|null $clock = null ]) : mixed
Parameters
- $config : array<string|int, mixed> = []
-
Configuration options
- $clock : IClock|null = null
-
Clock implementation (null = use real clock)
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
gc()
Garbage collection - remove expired files.
public
gc() : int
Return values
int —Number of files removed
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
getFilePath()
Get file path for a key.
private
getFilePath(string $key) : string
Parameters
- $key : string
Return values
stringreadFile()
Read data from file with locking.
private
readFile(string $filePath) : array<string|int, mixed>|null
Parameters
- $filePath : string
Return values
array<string|int, mixed>|nullwriteFile()
Write data to file with locking.
private
writeFile(string $filePath, array<string|int, mixed> $data) : bool
Parameters
- $filePath : string
- $data : array<string|int, mixed>