Skip to main content

A psycopg-Backed PostgreSQL Cache

Project description

psycache: psycopg-Backed PostgreSQL Cache

License: MIT Documentation PyPI version No AI slop inside.

A simple 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 once (python -Im psycache init-db <dsn> does the same from the shell), 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.

Project details


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.2.0.tar.gz (36.9 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.2.0-py3-none-any.whl (16.6 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for psycache-26.2.0.tar.gz
Algorithm Hash digest
SHA256 a9c42e165d25cadc6ed62d1eedb739f813eec4eb9b7901112f468322fb9d168c
MD5 634e87d7bcd1a69f231e964eadb37460
BLAKE2b-256 d786adfccca8ba515912d5fd44d21235e0f67a8d2476debd8a0495220e540c38

See more details on using hashes here.

Provenance

The following attestation bundles were made for psycache-26.2.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.2.0-py3-none-any.whl.

File metadata

  • Download URL: psycache-26.2.0-py3-none-any.whl
  • Upload date:
  • Size: 16.6 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.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d21e4647c1152f051914240cc6f8081becc5b5b716bbec32a5584197fa4e3f32
MD5 31261a979bc5f889dcb5a98f1af9cb92
BLAKE2b-256 f61f143e109bfaafef0001fd2ed4c6b6f9eb7e8060e3bcd9997977935904cb64

See more details on using hashes here.

Provenance

The following attestation bundles were made for psycache-26.2.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.

Supported by

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