|
| 1 | +# Backends |
| 2 | + |
| 3 | +MemState separates logic from storage. You can start with `InMemoryStorage` for testing and switch to `PostgresStorage` or `RedisStorage` for production without changing your agent logic. |
| 4 | + |
| 5 | +**Pro Tip: Dependency Injection** |
| 6 | + |
| 7 | +MemState fully supports **Dependency Injection**. For all backends (Postgres, Redis, SQLite), you can pass an existing connection object instead of a connection string. |
| 8 | + |
| 9 | +## PostgreSQL |
| 10 | + |
| 11 | +Uses `SQLAlchemy` + `psycopg`. It supports JSONB for efficient querying. |
| 12 | + |
| 13 | +### Install the requirements |
| 14 | + |
| 15 | +=== "uv" |
| 16 | + ```bash |
| 17 | + uv add memstate[postgres] |
| 18 | + ``` |
| 19 | + |
| 20 | +=== "pip" |
| 21 | + ```bash |
| 22 | + pip install memstate[postgres] |
| 23 | + ``` |
| 24 | + |
| 25 | +### Initialize the storage |
| 26 | + |
| 27 | +=== "sync" |
| 28 | + ```python |
| 29 | + from memstate import MemoryStore |
| 30 | + from memstate.backends.postgres import PostgresStorage |
| 31 | + |
| 32 | + url = "postgresql+psycopg://user:pass@localhost:5432/db_name" |
| 33 | + storage = PostgresStorage(url) |
| 34 | + store = MemoryStore(storage=storage) |
| 35 | + ``` |
| 36 | + |
| 37 | +=== "async" |
| 38 | + ```python |
| 39 | + import asyncio |
| 40 | + from memstate import AsyncMemoryStore |
| 41 | + from memstate.backends.postgres import AsyncPostgresStorage |
| 42 | + |
| 43 | + async def main(): |
| 44 | + url = "postgresql+psycopg://user:pass@localhost:5432/db_name" |
| 45 | + storage = AsyncPostgresStorage(url) |
| 46 | + # Important: You must create tables explicitly in async mode |
| 47 | + await store.create_tables() |
| 48 | + |
| 49 | + store = AsyncMemoryStore(storage=storage) |
| 50 | + |
| 51 | + if __name__ == "__main__": |
| 52 | + asyncio.run(main()) |
| 53 | + ``` |
| 54 | + |
| 55 | +## Redis |
| 56 | + |
| 57 | +Stores facts as JSON strings and maintains sets for efficient indexing. |
| 58 | + |
| 59 | +### Install the requirements |
| 60 | + |
| 61 | +=== "uv" |
| 62 | + ```bash |
| 63 | + uv add memstate[redis] |
| 64 | + ``` |
| 65 | + |
| 66 | +=== "pip" |
| 67 | + ```bash |
| 68 | + pip install memstate[redis] |
| 69 | + ``` |
| 70 | + |
| 71 | +### Initialize the storage |
| 72 | + |
| 73 | +=== "sync" |
| 74 | + ```python |
| 75 | + from memstate import MemoryStore |
| 76 | + from memstate.backends.redis import RedisStorage |
| 77 | + |
| 78 | + storage = RedisStorage("redis://localhost:6379/0") |
| 79 | + store = MemoryStore(storage=storage) |
| 80 | + ``` |
| 81 | + |
| 82 | +=== "async" |
| 83 | + ```python |
| 84 | + import asyncio |
| 85 | + from memstate import AsyncMemoryStore |
| 86 | + from memstate.backends.redis import AsyncRedisStorage |
| 87 | + |
| 88 | + async def main(): |
| 89 | + storage = AsyncRedisStorage("redis://localhost:6379/0") |
| 90 | + store = AsyncMemoryStore(storage=storage) |
| 91 | + |
| 92 | + if __name__ == "__main__": |
| 93 | + asyncio.run(main()) |
| 94 | + ``` |
| 95 | + |
| 96 | +## SQLite |
| 97 | + |
| 98 | +The default, zero-config backend. Uses the JSON1 extension for querying. |
| 99 | + |
| 100 | +### Install the requirements |
| 101 | + |
| 102 | +For synchronous use, SQLite is built-in. |
| 103 | + |
| 104 | +For **async** use, you need `aiosqlite`. |
| 105 | + |
| 106 | +=== "uv" |
| 107 | + ```bash |
| 108 | + uv add memstate[sqlite-async] |
| 109 | + ``` |
| 110 | + |
| 111 | +=== "pip" |
| 112 | + ```bash |
| 113 | + pip install memstate[sqlite-async] |
| 114 | + ``` |
| 115 | + |
| 116 | +### Initialize the storage |
| 117 | + |
| 118 | +=== "sync" |
| 119 | + ```python |
| 120 | + from memstate import MemoryStore, SQLiteStorage |
| 121 | + |
| 122 | + storage = SQLiteStorage("memory.db") |
| 123 | + store = MemoryStore(storage=storage) |
| 124 | + ``` |
| 125 | + |
| 126 | +=== "async" |
| 127 | + ```python |
| 128 | + import asyncio |
| 129 | + from memstate import AsyncMemoryStore, AsyncSQLiteStorage |
| 130 | + |
| 131 | + async def main(): |
| 132 | + storage = AsyncSQLiteStorage("memory.db") |
| 133 | + # Important: Connect explicitly |
| 134 | + await storage.connect() |
| 135 | + |
| 136 | + store = AsyncMemoryStore(storage=storage) |
| 137 | + |
| 138 | + if __name__ == "__main__": |
| 139 | + asyncio.run(main()) |
| 140 | + ``` |
| 141 | + |
| 142 | +## In-memory |
| 143 | + |
| 144 | +Non-persistent storage. Best for testing and prototyping. |
| 145 | + |
| 146 | +### Initialize the storage |
| 147 | + |
| 148 | +=== "sync" |
| 149 | + ```python |
| 150 | + from memstate import MemoryStore, InMemoryStorage |
| 151 | + |
| 152 | + storage = InMemoryStorage() |
| 153 | + store = MemoryStore(storage=storage) |
| 154 | + ``` |
| 155 | + |
| 156 | +=== "async" |
| 157 | + ```python |
| 158 | + import asyncio |
| 159 | + from memstate import AsyncMemoryStore, AsyncInMemoryStorage |
| 160 | + |
| 161 | + async def main(): |
| 162 | + storage = AsyncInMemoryStorage() |
| 163 | + store = AsyncMemoryStore(storage=storage) |
| 164 | + |
| 165 | + if __name__ == "__main__": |
| 166 | + asyncio.run(main()) |
| 167 | + ``` |
0 commit comments