RedisStorage
extends BaseStorage
in package
Redis-backed FSM storage using `amphp/redis ^2`.
Mirrors aiogram.fsm.storage.redis.RedisStorage
(aiogram/fsm/storage/redis.py). State and data are stored as plain
Redis strings; data payloads are JSON-serialised.
TTLs are expressed in seconds (matching the upstream state_ttl /
data_ttl datetime.timedelta-derived semantics). null means
no expiry — keys persist until explicitly deleted or the server is
flushed.
The fromUrl factory mirrors upstream RedisStorage.from_url and
provides a convenient single-string construction path compatible with
any URI accepted by Amp\Redis\RedisConfig::fromUri:
redis://[:password@]host[:port][/db]tcp://host:portunix:///path/to/socket
Key building delegates to the injected KeyBuilder; the default is
DefaultKeyBuilder (prefix fsm, colon-separated segments).
Table of Contents
Properties
- $dataTtl : int|null
- $keyBuilder : KeyBuilder
- $redis : RedisClient
- $stateTtl : int|null
Methods
- __construct() : mixed
- close() : void
- Close the Redis connection.
- fromUrl() : self
- Create a `RedisStorage` from a URI string.
- getData() : array<string, mixed>
- Retrieve the FSM data payload for the given key.
- getState() : null|string
- Retrieve the FSM state for the given key.
- getValue() : mixed
- Read a single key from the data map stored at `$storageKey`.
- setData() : void
- Persist the FSM data payload for the given key.
- setState() : void
- Persist the FSM state for the given key.
- updateData() : array<string, mixed>
- Merge `$data` into the existing record at `$key` and persist the result.
Properties
$dataTtl read-only
private
int|null
$dataTtl
= null
$keyBuilder read-only
private
KeyBuilder
$keyBuilder
= new DefaultKeyBuilder()
$redis read-only
private
RedisClient
$redis
$stateTtl read-only
private
int|null
$stateTtl
= null
Methods
__construct()
public
__construct(RedisClient $redis[, KeyBuilder $keyBuilder = new DefaultKeyBuilder() ][, null|int $stateTtl = null ][, null|int $dataTtl = null ]) : mixed
Parameters
- $redis : RedisClient
-
amphp/redis client instance.
- $keyBuilder : KeyBuilder = new DefaultKeyBuilder()
-
Key-builder strategy.
- $stateTtl : null|int = null
-
TTL in seconds for state keys, or
nullfor no expiry. - $dataTtl : null|int = null
-
TTL in seconds for data keys, or
nullfor no expiry.
close()
Close the Redis connection.
public
close() : void
Mirrors RedisStorage.close (upstream redis.py).
Calls quit() on the client to gracefully disconnect.
fromUrl()
Create a `RedisStorage` from a URI string.
public
static fromUrl(string $url[, null|KeyBuilder $keyBuilder = null ][, null|int $stateTtl = null ][, null|int $dataTtl = null ]) : self
Mirrors RedisStorage.from_url (upstream redis.py). Internally
calls Amp\Redis\createRedisClient() which wraps
ReconnectingRedisLink + SocketRedisConnector derived from
RedisConfig::fromUri($url).
Parameters
- $url : string
-
Redis URI (e.g.
redis://localhost:6379/0). - $keyBuilder : null|KeyBuilder = null
-
Optional key-builder override.
- $stateTtl : null|int = null
-
TTL in seconds for state keys.
- $dataTtl : null|int = null
-
TTL in seconds for data keys.
Return values
selfgetData()
Retrieve the FSM data payload for the given key.
public
getData(StorageKey $key) : array<string, mixed>
Mirrors RedisStorage.get_data (upstream redis.py).
Parameters
- $key : StorageKey
-
Storage address.
Tags
Return values
array<string, mixed> —Current data map (empty array when no data stored).
getState()
Retrieve the FSM state for the given key.
public
getState(StorageKey $key) : null|string
Mirrors RedisStorage.get_state (upstream redis.py).
Parameters
- $key : StorageKey
-
Storage address.
Return values
null|string —Stored state name, or null if none.
getValue()
Read a single key from the data map stored at `$storageKey`.
public
getValue(StorageKey $storageKey, string $dictKey[, mixed $default = null ]) : mixed
Returns $default when the dict key is absent. PHP collapses the
upstream @overload pattern into one method with a mixed $default = null
parameter.
Parameters
- $storageKey : StorageKey
-
Storage address.
- $dictKey : string
-
Key within the data map.
- $default : mixed = null
-
Value to return when
$dictKeyis not present.
Return values
mixed —The stored value, or $default.
Mirrors BaseStorage.get_value (base.py:151-168).
setData()
Persist the FSM data payload for the given key.
public
setData(StorageKey $key, array<string, mixed> $data) : void
Mirrors RedisStorage.set_data (upstream redis.py).
An empty array results in the Redis key being deleted (matching
upstream behaviour: if not data: await self.redis.delete(redis_key)).
Parameters
- $key : StorageKey
-
Storage address.
- $data : array<string, mixed>
-
Data map to store.
Tags
setState()
Persist the FSM state for the given key.
public
setState(StorageKey $key[, null|State|string $state = null ]) : void
Mirrors RedisStorage.set_state (upstream redis.py).
$state === null→ delete the key.$state instanceof State→ store$state->state().$stateis a plain string → store as-is.
Parameters
- $key : StorageKey
-
Storage address.
- $state : null|State|string = null
-
New state value.
updateData()
Merge `$data` into the existing record at `$key` and persist the result.
public
updateData(StorageKey $key, array<string, mixed> $data) : array<string, mixed>
Loads the current data map, merges the supplied patch on top with
array_merge (later keys win on collision), persists the merged map,
and returns a copy so that mutations to the returned array cannot bleed
back into storage.
Parameters
- $key : StorageKey
-
Storage address.
- $data : array<string, mixed>
-
Partial data map to merge in.
Return values
array<string, mixed> —A copy of the merged data map as it was persisted.
Mirrors BaseStorage.update_data (base.py:170-181).
Upstream returns current_data.copy() after in-place update.