Documentation

RateLimitStorage

Storage backend for rate limit data.

Uses APCu if available, otherwise falls back to file-based storage. Automatically cleans up expired entries.

Tags
category

Lwt

author

HugoFara hugo.farajallah@protonmail.com

license

Unlicense http://unlicense.org/

link
https://hugofara.github.io/lwt/docs/php/
since
3.0.0

Table of Contents

Constants

CLEANUP_PROBABILITY  = 100
Cleanup probability (1 in N requests triggers cleanup).

Properties

$storageDir  : string
Storage directory for file-based backend.
$useApcu  : bool
Whether APCu is available.

Methods

__construct()  : mixed
Create a new RateLimitStorage.
clear()  : void
Clear all rate limit data (for testing).
get()  : array{count: int, window_start: int}|null
Get rate limit data for a key.
set()  : void
Set rate limit data for a key.
cleanup()  : void
Clean up expired entries from file storage.
getDefaultStorageDir()  : string
Get the default storage directory.
getFilePath()  : string
Get the file path for a storage key.
getFromFile()  : array{count: int, window_start: int}|null
Get data from file storage.
saveToFile()  : void
Save data to file storage.

Constants

CLEANUP_PROBABILITY

Cleanup probability (1 in N requests triggers cleanup).

private mixed CLEANUP_PROBABILITY = 100

Properties

$storageDir

Storage directory for file-based backend.

private string $storageDir

Methods

__construct()

Create a new RateLimitStorage.

public __construct([string|null $storageDir = null ]) : mixed
Parameters
$storageDir : string|null = null

Optional custom storage directory

clear()

Clear all rate limit data (for testing).

public clear() : void
Tags
psalm-suppress

PossiblyUnusedMethod - Public API for testing

get()

Get rate limit data for a key.

public get(string $key) : array{count: int, window_start: int}|null
Parameters
$key : string

Storage key

Return values
array{count: int, window_start: int}|null

Data or null if not found

set()

Set rate limit data for a key.

public set(string $key, array<string|int, mixed> $data, int $ttl) : void
Parameters
$key : string

Storage key

$data : array<string|int, mixed>

Rate limit data

$ttl : int

Time-to-live in seconds

cleanup()

Clean up expired entries from file storage.

private cleanup() : void

getDefaultStorageDir()

Get the default storage directory.

private getDefaultStorageDir() : string
Return values
string

Path to storage directory

getFilePath()

Get the file path for a storage key.

private getFilePath(string $key) : string
Parameters
$key : string

Storage key

Return values
string

File path

getFromFile()

Get data from file storage.

private getFromFile(string $key) : array{count: int, window_start: int}|null
Parameters
$key : string

Storage key

Return values
array{count: int, window_start: int}|null

Data or null if not found/expired

saveToFile()

Save data to file storage.

private saveToFile(string $key, array<string|int, mixed> $data, int $ttl) : void
Parameters
$key : string

Storage key

$data : array<string|int, mixed>

Rate limit data

$ttl : int

Time-to-live in seconds


        
On this page

Search results