NAME

PAGI::FastAPI::RateLimit::Driver::Memory - Default In-Memory Storage Driver for Rate Limiting

VERSION

Version v1.0.0

SYNOPSIS

use PAGI::FastAPI::RateLimit::Driver::Memory;

my $driver = PAGI::FastAPI::RateLimit::Driver::Memory->new();

# Increment hit count for key 'client_127.0.0.1' with a 60-second window
$driver->increment_async('client_127.0.0.1', 60)->then(sub ($count, $expires_at) {
    print "Current request count: $count (resets at $expires_at)\n";
});

# Retrieve current hits
$driver->get_async('client_127.0.0.1')->then(sub ($count) {
    print "Hits: $count\n";
});

# Reset quota
$driver->reset_async('client_127.0.0.1');

DESCRIPTION

PAGI::FastAPI::RateLimit::Driver::Memory is the default, in-memory implementation of PAGI::FastAPI::RateLimit::Driver. It ships directly with the core PAGI::FastAPI distribution.

It tracks request hits and window expirations per key inside an in-process hash table. Expired keys are automatically purged on access.

Note: Storage is maintained entirely in process memory. Hit counters are not shared across multi-process workers (e.g., Starman, Hypnotoad) or distributed server environments. For production clusters, install a distributed storage driver such as PAGI::FastAPI::RateLimit::Driver::Redis.

METHODS

Inherits all methods from PAGI::FastAPI::RateLimit::Driver.

new()

Instantiates a new in-memory rate limiting driver. Takes no required arguments.

increment_async($key, $ttl)

Purges any expired record for $key, increments its counter, sets the expiration timestamp if missing, and returns a Future resolving to a two-element list ($count, $expires_at): the updated integer hit count and the Unix epoch timestamp at which the window resets. $expires_at is set once, on the first hit of a window, and held steady for the rest of that window.

get_async($key)

Purges any expired record for $key and returns a Future resolving to its current integer hit count (or 0 if absent/expired).

reset_async($key)

Removes $key from internal storage and returns a Future resolving to 1.

SEE ALSO

PAGI::FastAPI::RateLimit::Driver, PAGI::FastAPI::Middleware::RateLimit

AUTHOR

Mohammad Sajid Anwar, <mohammad.anwar at yahoo.com>

REPOSITORY

https://github.com/manwar/PAGI-FastAPI

BUGS

Please report any bugs or feature requests through the web interface at https://github.com/manwar/PAGI-FastAPI/issues. I will be notified and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

You can find documentation for this module with the perldoc command.

perldoc PAGI::FastAPI::RateLimit::Driver::Memory

You can also look for information at:

LICENSE AND COPYRIGHT

Copyright (C) 2026 Mohammad Sajid Anwar.

This program is free software; you can redistribute it and/or modify it under the terms of the Artistic License (2.0).