Skip to main content

psycache

A Psycopg-backed PostgreSQL cache

A key-value cache that stores JSON in PostgreSQL through Psycopg 3, with TTL-based expiration and pluggable instrumentation.


psycache uses an unlogged table for performance and stores values as JSONB for versatility.

It's a great fit when you already have PostgreSQL and need a fast cache without introducing another piece of infrastructure like Redis. For example, you can safely share a SQLAlchemy Engine (or AsyncEngine) with psycache.

Quick Start

Let's hitch-hike on a SQLAlchemy engine as a quick example!

First, install psycache from PyPI with the sqlalchemy extra:

$ uv pip install "psycache[sqlalchemy]"

Initialize the cache table once1 then store and retrieve JSON with a TTL:

import psycopg

from sqlalchemy import create_engine

import psycache

from psycache import PostgresCache
from psycache.sqlalchemy import SQLAlchemyCachePool


with psycopg.connect(
    "postgresql://psycache@127.0.0.1/psycache", autocommit=True
) as conn:
    psycache.init_db(conn)

engine = create_engine("postgresql+psycopg://psycache@127.0.0.1/psycache")
cache = PostgresCache(SQLAlchemyCachePool(engine))

cache.put_raw("user:alice", {"score": 42}, ttl=300)
value = cache.get_raw("user:alice")
# {"score": 42}

engine.dispose()

Documentation

Full documentation lives at https://psycache.hynek.me/.

Release Information

Changed

  • psycache is now considered stable and the usual backwards-compatibility promises apply.

  • The schema parameters of init_db(), PostgresCache, and AsyncPostgresCache and the --schema option of init-db have been replaced by table and --table. They take the name of the cache table, optionally schema-qualified with a dot (for example, app_cache.psycache).

Fixed

  • sqlalchemy: Imports from sqlalchemy.ext.asyncio are now protected using if TYPE_CHECKING:. This fixes the runtime dependency on greenlet on SQLALchemy 2.1+, even if psycache.sqlalchemy.AsyncSQLAlchemyCachePool is not used. #11

Full Changelog →

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.

  1. python -Im psycache init-db <dsn> does the same from the shell. Add --table <table> to use a different table name, optionally schema-qualified with a dot. Omit <dsn> to print the SQL. ↩

Metadata

Release files for psycache 26.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for psycache 26.4.0
File Size Uploaded Provenance
psycache-26.4.0.tar.gz 41.3 kB With provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for psycache 26.4.0
File Interpreter ABI Platform
psycache-26.4.0-py3-none-any.whl Python 3 none any Details

Total release size: 59.2 kB

Release files / psycache-26.4.0.tar.gz

Download URL psycache-26.4.0.tar.gz
Size 41.3 kB
Tags Source
SHA-256 checksum
How to use checksums
81c0a5284ee26b8fb18fc36fc55edf793f39c6899e3885675d5a0cf8f8b9ca55
BLAKE2b-256 checksum
How to use checksums
1cfcce8cd180b6c639db6cb53ee579bccb78bc126bee69e36e7367a3759aa6f3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 29, 2026.

Transparency log

Release files / psycache-26.4.0-py3-none-any.whl

Download URL psycache-26.4.0-py3-none-any.whl
Size 17.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2a0ba0c1d4c7ab8a871472a75b6ff3ff8cda0bbfb348b265045bf2bf519267f5
BLAKE2b-256 checksum
How to use checksums
597982fd101ed0e98e64308a5317b94b40dd9a7421b2b8bc3e8f178ce3a1324e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 29, 2026.

Transparency log

Security for psycache 26.4.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for psycache-26.4.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 29, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page