PdoRateLimitStore
in package
implements
RateLimitStoreInterface
Table of Contents
Interfaces
Properties
- $pdo : PDO
Methods
- __construct() : mixed
- Store the PDO connection and ensure the rate_limits table exists.
- allForClient() : array<string, array{remaining: int, timestamp: int}>
- Fetches all rate-limit entries for a client, indexed by window.
- get() : null|array{remaining: int, timestamp: int}
- Fetches the remaining token count and expiry timestamp for a client's rate-limit window.
- set() : void
- Store or update the rate-limit record for a client and window.
- init() : void
- Ensure the `rate_limits` table exists in the connected PDO database.
Properties
$pdo
private
PDO
$pdo
Methods
__construct()
Store the PDO connection and ensure the rate_limits table exists.
public
__construct(PDO $pdo) : mixed
Initializes the store by saving the provided PDO instance and creating the
rate_limits table if it does not already exist.
Parameters
- $pdo : PDO
allForClient()
Fetches all rate-limit entries for a client, indexed by window.
public
allForClient(string $clientId) : array<string, array{remaining: int, timestamp: int}>
Parameters
- $clientId : string
-
the client identifier
Return values
array<string, array{remaining: int, timestamp: int}> —associative array keyed by window name; each value contains remaining (int) and timestamp (int)
get()
Fetches the remaining token count and expiry timestamp for a client's rate-limit window.
public
get(string $clientId, string $window) : null|array{remaining: int, timestamp: int}
Parameters
- $clientId : string
-
identifier of the client
- $window : string
-
identifier of the rate-limit window
Return values
null|array{remaining: int, timestamp: int} —the row for the specified client and window with integer remaining and timestamp, or null if no record exists
set()
Store or update the rate-limit record for a client and window.
public
set(string $clientId, string $window, int $remaining, int $timestamp) : void
Parameters
- $clientId : string
-
identifier of the client
- $window : string
-
Identifier of the rate-limit window (e.g., "1m", "hourly").
- $remaining : int
-
number of remaining allowed requests for the window
- $timestamp : int
-
UNIX timestamp associated with the record (seconds since epoch)
init()
Ensure the `rate_limits` table exists in the connected PDO database.
private
init() : void
Creates a table with columns: client_id (TEXT), window (TEXT), remaining (INTEGER),
timestamp (INTEGER) and a composite primary key on (client_id, window).