Skip to content

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: PostgresCache and a fully async AsyncPostgresCache that 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

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.