A psycopg-Backed PostgreSQL Cache
Project description
psycache: psycopg-Backed PostgreSQL Cache
A simple key-value cache that stores JSON in PostgreSQL through psycopg 3, with TTL-based expiration and pluggable instrumentation.
- Sync and async ✔︎
- Type-safe ✔︎
- Adapters for SQLAlchemy and psycopg-pool ✔︎
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a9c42e165d25cadc6ed62d1eedb739f813eec4eb9b7901112f468322fb9d168c
|
|
| MD5 |
634e87d7bcd1a69f231e964eadb37460
|
|
| BLAKE2b-256 |
d786adfccca8ba515912d5fd44d21235e0f67a8d2476debd8a0495220e540c38
|
Provenance
The following attestation bundles were made for psycache-26.2.0.tar.gz:
Publisher:
pypi-package.yml on hynek/psycache
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
psycache-26.2.0.tar.gz -
Subject digest:
a9c42e165d25cadc6ed62d1eedb739f813eec4eb9b7901112f468322fb9d168c - Sigstore transparency entry: 1952845309
- Sigstore integration time:
-
Permalink:
hynek/psycache@6eca5cb4bc4a7978243f0bfc51aebff494812426 -
Branch / Tag:
refs/tags/26.2.0 - Owner: https://github.com/hynek
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi-package.yml@6eca5cb4bc4a7978243f0bfc51aebff494812426 -
Trigger Event:
release
-
Statement type:
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d21e4647c1152f051914240cc6f8081becc5b5b716bbec32a5584197fa4e3f32
|
|
| MD5 |
31261a979bc5f889dcb5a98f1af9cb92
|
|
| BLAKE2b-256 |
f61f143e109bfaafef0001fd2ed4c6b6f9eb7e8060e3bcd9997977935904cb64
|
Provenance
The following attestation bundles were made for psycache-26.2.0-py3-none-any.whl:
Publisher:
pypi-package.yml on hynek/psycache
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
psycache-26.2.0-py3-none-any.whl -
Subject digest:
d21e4647c1152f051914240cc6f8081becc5b5b716bbec32a5584197fa4e3f32 - Sigstore transparency entry: 1952845719
- Sigstore integration time:
-
Permalink:
hynek/psycache@6eca5cb4bc4a7978243f0bfc51aebff494812426 -
Branch / Tag:
refs/tags/26.2.0 - Owner: https://github.com/hynek
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi-package.yml@6eca5cb4bc4a7978243f0bfc51aebff494812426 -
Trigger Event:
release
-
Statement type: