Skip to main content

psycache: psycopg-Backed PostgreSQL Cache

License: MIT Documentation PyPI version No AI slop inside.

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/.

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 --schema <schema> to use a non-default PostgreSQL schema. Omit <dsn> to print the SQL.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

psycache-26.3.0.tar.gz (38.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

psycache-26.3.0-py3-none-any.whl (17.5 kB view details)

Uploaded Python 3

File details

Details for the file psycache-26.3.0.tar.gz.

File metadata

  • Download URL: psycache-26.3.0.tar.gz
  • Upload date:
  • Size: 38.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for psycache-26.3.0.tar.gz
Algorithm Hash digest
SHA256 dcdb53d5803706bfcdc5ba45e76bad5b84878af657345b1a05f3666ec2ceeb30
MD5 3a9aba9563e6615ea127555e18a0f0cd
BLAKE2b-256 bed50716e95a55d1a4cbd8fa55d0bbe02fe363d6b6bedb8a75adb3b9eb24fde1

See more details on using hashes here.

Provenance

The following attestation bundles were made for psycache-26.3.0.tar.gz:

Publisher: pypi-package.yml on hynek/psycache

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file psycache-26.3.0-py3-none-any.whl.

File metadata

  • Download URL: psycache-26.3.0-py3-none-any.whl
  • Upload date:
  • Size: 17.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for psycache-26.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f1eb776c3ca45b8d6cba053c30c07e858a6e386e3ef0aa8c3e4dab3497dd3428
MD5 b69a9e2692ed9a96123d258b6e88b9ab
BLAKE2b-256 95aa5689c7f823b31e7daa4ff61ae61dd53d62f472b2e6666802edea67eb3eeb

See more details on using hashes here.

Provenance

The following attestation bundles were made for psycache-26.3.0-py3-none-any.whl:

Publisher: pypi-package.yml on hynek/psycache

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

This release

26.3.0 This release

2 files

26.2.0

2 files

26.1.0

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page