MemoryRateLimitStorage
in package
implements
IRateLimitStorage
In-memory rate limit storage for testing.
WARNING: This storage backend is only suitable for testing as data is lost when the PHP process terminates. Use Redis or File storage for production deployments.
Table of Contents
Interfaces
- IRateLimitStorage
- Interface for rate limit storage implementations.
Properties
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).
- getRemainingAttempts() : int
- Get the number of remaining attempts for a key.
- getResetTime() : int
- Get the timestamp when the rate limit window resets.
- getStorage() : array<string|int, mixed>
- Get current storage state (for testing).
- reset() : void
- Reset the rate limit counter for a specific key.
Properties
$_prefix
private
string
$_prefix
$_storage
private
array<string|int, mixed>
$_storage
= []
$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
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
getStorage()
Get current storage state (for testing).
public
getStorage() : array<string|int, mixed>
Return values
array<string|int, mixed>reset()
Reset the rate limit counter for a specific key.
public
reset(string $key) : void
Parameters
- $key : string
-
The unique identifier to reset