v0.3.x · pyrogram_patch
Storage Backends
FSM state and data are persisted through a BaseStorage backend. v0.3 ships two built-in backends: MemoryStorage (in-process, volatile) and RedisStorage (persistent, shareable across processes).
MemoryStorage
Stores state in a Python dict. Lost on restart. Best for development and single-process bots with no persistence requirement.
from pyrogram_patch.fsm.storages import MemoryStorage
storage = MemoryStorage()
manager = patch(app, storage=storage)| Parameter | Type | Default | Description |
|---|---|---|---|
ttl | int | None | None | Default TTL in seconds for all keys |
RedisStorage
Stores state in Redis. Survives restarts and works across multiple bot processes. Requires pip install kurigram-addons[redis].
from pyrogram_patch.fsm.storages.redis_storage import RedisStorage
storage = RedisStorage(
host="localhost",
port=6379,
db=0,
prefix="mybot", # optional key namespace
ttl=3600, # optional default TTL
)
manager = patch(app, storage=storage)Custom Storage
Implement BaseStorage to use any backend (PostgreSQL, MongoDB, etc.).
from pyrogram_patch.fsm.base_storage import BaseStorage
class MyStorage(BaseStorage):
async def get_state(self, identifier: str):
...
async def set_state(self, identifier: str, state, *, ttl=None):
...
async def delete_state(self, identifier: str) -> bool:
...
async def compare_and_set(self, identifier, new_state, *, expected_state, ttl=None) -> bool:
...