library for persisting and caching data
Actively maintained. Last release 2 months ago, commits in 17 of the last 26 weeks. A safe choice.
pip install persistalibrary for persisting and caching data
Actively maintained. Last release 2 months ago, commits in 17 of the last 26 weeks. A safe choice.
pip install persistapersista is a lightweight Python library that provides simple, consistent building blocks for
persisting and caching data. It offers a uniform key-value store interface backed by multiple
storage engines (in-memory, SQLite, DuckDB, LMDB, Redis, PostgreSQL) with both sync and async
APIs, plus TTL caching and HTTP fetch utilities.
Quick Links:
Storage backends have different APIs, and switching between them (e.g. moving from an in-memory
store in tests to Redis or PostgreSQL in production) usually means rewriting code. persista
solves this with a single, consistent BaseStore interface:
Store and retrieve values:
>>> from persista.store import InMemoryStore
>>> with InMemoryStore() as store:
... store.set("user:1", {"name": "Alice"})
... store.get("user:1")
...
{'name': 'Alice'}
Swap the backend without changing the calling code:
>>> import tempfile
>>> from pathlib import Path
>>> from persista.store import SQLiteStore
>>> with tempfile.TemporaryDirectory() as tmpdir:
... with SQLiteStore(Path(tmpdir).joinpath("data.sqlite")) as store:
... store.set("user:1", {"name": "Alice"})
... store.get("user:1")
...
{'name': 'Alice'}
Cache expensive calls with a TTL:
>>> from persista.cache import cached
>>> @cached(ttl=60)
... def slow_call(x: int) -> int:
... return x**2
...
>>> slow_call(4)
16
See the documentation for detailed examples.
persista provides a comprehensive set of utilities for persisting and caching data:
A consistent BaseStore interface for storing dict values under string keys, with both
synchronous and a-prefixed asynchronous methods on every store:
get/aget, get_many/aget_many, set/aset,
set_many/aset_many, delete/adelete, filter/afilter, iterationInMemoryStore, SQLiteStore, DuckDBStore, LmdbStore, RedisStore, PostgresStoreTypedSQLiteStore, TypedPostgresStore, ...) and pickle-backed variants
(PickleLmdbStore, PickleRedisStore) for non-dict values"raise", "skip", "overwrite", "merge")Time-to-live caching for functions and values, with sync and async variants:
Cache, with sync and async (a-prefixed) methods, for explicit cache instancescached / async_cached decorators for caching function callsget_cacheHelpers to fetch HTTP responses with automatic retries, built on top of requests or httpx:
fetch_response (sync, requests); get_response/post_response/put_response/
patch_response/delete_response/send_request (sync, httpx) and their _async
counterparts (async, httpx)HttpClient/AsyncHttpClient: class-based wrappers around httpx.Client/httpx.AsyncClient
with the same retries, plus optional response caching via a CacheWe highly recommend installing persista in
a virtual environment
to avoid dependency conflicts.
uv is a fast Python package installer and resolver:
uv pip install persista
Install with specific optional dependencies:
uv pip install persista[redis,httpx] # with Redis and httpx support
Alternatively, you can use pip:
pip install persista
Install with specific optional dependencies:
pip install persista[redis,httpx] # with Redis and httpx support
coolaOptional dependencies, enabled per-backend:
| Extra | Enables |
|---|---|
aiosqlite | Async SQLite store |
duckdb | DuckDB store |
faker | Test data generation helpers |
httpx | Sync/async HTTP fetch utilities |
lmdb | LMDB store |
psycopg | PostgreSQL store |
redis | Redis store |
requests | Sync HTTP fetch utilities |
rich | Rich-formatted output |
urllib3 | urllib3-based HTTP utilities |
For detailed installation instructions, see the documentation.
Contributions are welcome! We appreciate bug fixes, feature additions, documentation improvements, and more. Please check the contributing guidelines for details on:
Whether you're fixing a bug or proposing a new feature, please open an issue first to discuss your changes.
:warning: Important: As persista is under active development, its API is not yet stable and may
change between releases. We recommend pinning a specific version in your project’s dependencies to
ensure consistent behavior.
persista is licensed under BSD 3-Clause "New" or "Revised" license available in LICENSE
file.
Use instead of persista: tools that do the same job, closest first.
Compare with py-key-value-aio →Compare with sqlitedict →Compare with aiocache →Compare all 4 →
Async Key-Value Store - A pluggable interface for KV Stores
Persistent dict in Python, backed up by sqlite3 and pickle, multithread-safe.
multi backend asyncio cache
Add your description here
persistent caching to memory, disk, or database
A key-value storage for binary data, support many backends.
Use together with persista: extensions, drivers, servers and companion tools.