psycache¶
A psycopg-backed PostgreSQL cache.
psycache is a simple key-value cache that stores JSON in PostgreSQL through psycopg 3, with TTL-based expiration, and pluggable instrumentation.
-
Sync and async:
PostgresCacheand a fully asyncAsyncPostgresCachethat mirrors its API. -
Type-safe: fully typed, checked using Mypy, ty, Pyrefly, and Pyright.
-
Bring your own pool: first-class adapters for SQLAlchemy and psycopg-pool, or implement a tiny protocol (one method!) yourself.
Why PostgreSQL?¶
Modern PostgreSQL on modern servers is fast enough for almost everyone’s use cases.
psycache stores values as JSONB in an unlogged table. Unlogged tables skip the write-ahead log for speed, while JSONB keeps values flexible and queryable.
It’s a great fit when you already run PostgreSQL and want a fast cache without operating another piece of infrastructure like Redis.
You can even share an existing SQLAlchemy Engine (or AsyncEngine) with psycache, so the cache rides on the connections you already have.
Get started¶
- Getting Started: install psycache, initialize the table, and store your first value.
- Raw Queries: Our low-level API:
get_raw,put_raw, TTLs, and instrumentation span names. - Connection Pool Adapters: wire up SQLAlchemy, psycopg-pool, or your own pool.
- Quality of Life: embrace the simple life.
Credits¶
psycache is written by Hynek Schlawack and distributed under the terms of the MIT license.
The development is kindly supported by my employer Variomedia AG and all my fabulous GitHub Sponsors.