Storages#
Storages out of the box#
MemoryStorage#
RedisStorage#
- class aiogram.fsm.storage.redis.RedisStorage(redis: ~redis.asyncio.client.Redis, key_builder: ~typing.Optional[~aiogram.fsm.storage.redis.KeyBuilder] = None, state_ttl: ~typing.Optional[~typing.Union[int, ~datetime.timedelta]] = None, data_ttl: ~typing.Optional[~typing.Union[int, ~datetime.timedelta]] = None, json_loads: ~typing.Callable[[...], ~typing.Any] = <function loads>, json_dumps: ~typing.Callable[[...], str] = <function dumps>)[source]#
Redis storage required
redis
package installed (pip install redis
)- __init__(redis: ~redis.asyncio.client.Redis, key_builder: ~typing.Optional[~aiogram.fsm.storage.redis.KeyBuilder] = None, state_ttl: ~typing.Optional[~typing.Union[int, ~datetime.timedelta]] = None, data_ttl: ~typing.Optional[~typing.Union[int, ~datetime.timedelta]] = None, json_loads: ~typing.Callable[[...], ~typing.Any] = <function loads>, json_dumps: ~typing.Callable[[...], str] = <function dumps>) None [source]#
- Parameters:
redis – Instance of Redis connection
key_builder – builder that helps to convert contextual key to string
state_ttl – TTL for state records
data_ttl – TTL for data records
- classmethod from_url(url: str, connection_kwargs: Optional[Dict[str, Any]] = None, **kwargs: Any) RedisStorage [source]#
Create an instance of
RedisStorage
with specifying the connection string- Parameters:
url – for example
redis://user:password@host:port/db
connection_kwargs – see
redis
docskwargs – arguments to be passed to
RedisStorage
- Returns:
an instance of
RedisStorage
Keys inside storage can be customized via key builders:
- class aiogram.fsm.storage.redis.DefaultKeyBuilder(*, prefix: str = 'fsm', separator: str = ':', with_bot_id: bool = False, with_destiny: bool = False)[source]#
Simple Redis key builder with default prefix.
Generates a colon-joined string with prefix, chat_id, user_id, optional bot_id and optional destiny.
Writing own storages#
- class aiogram.fsm.storage.base.BaseStorage[source]#
Base class for all FSM storages
- abstract async set_state(key: StorageKey, state: Optional[Union[str, State]] = None) None [source]#
Set state for specified key
- Parameters:
key – storage key
state – new state
- abstract async get_state(key: StorageKey) Optional[str] [source]#
Get key state
- Parameters:
key – storage key
- Returns:
current state
- abstract async set_data(key: StorageKey, data: Dict[str, Any]) None [source]#
Write data (replace)
- Parameters:
key – storage key
data – new data
- abstract async get_data(key: StorageKey) Dict[str, Any] [source]#
Get current data for key
- Parameters:
key – storage key
- Returns:
current data