PostgreSQL as a cache for Python applications
Project description
postgres-cache
Postgres Cache is a single purpose library to cover a need of a distributed cache and not willing to use Redis/Valkey/Memcached or other external caching systems.
Idea of the library is to store persisted cache in Postgres, and utilize couple of specific optimizations to make it behave comparable with other caching solutions.
Features
-
Pure Postgres backend – no bespoke services, just a single table plus triggers and LISTEN/NOTIFY for invalidations.
-
TTL + forced invalidation – each entry stores an optional
expires_at, and clients can delete rows explicitly. -
Dogpile protection –
get_or_setuses PostgreSQL advisory locks so only one client recomputes missing items. -
Local in-memory cache – every client keeps a bounded TTL cache that is updated/evicted via database notifications.
-
Configurable Postgres objects –
notify_channelcontrols the LISTEN/NOTIFY channel, andschema_prefixnamespaces every table/function created by the library. -
Optional notifications – set
disable_notiffy=Trueto skip LISTEN/NOTIFY when Postgres connection budgets are tight. -
Load harness – runnable script that spins up many clients to assert read consistency and provide latency stats.
Usage
PostgreSQL-backed cache
import asyncio
from postgres_cache import CacheSettings, PostgresCache
async def main():
dsn = "postgresql://cache_user:cache_pass@localhost:5432/cache_proto"
settings = CacheSettings(
dsn=dsn,
notify_channel="cache_events_marketing",
schema_prefix="marketing_",
)
await PostgresCache.init_db(settings)
async with PostgresCache(settings) as cache:
await cache.set("profile:123", {"name": "Ava"}, ttl_seconds=300)
user = await cache.get_or_set(
"profile:456",
loader=lambda: {"name": "Nova"},
ttl_seconds=120,
)
print(user)
asyncio.run(main())
The clients automatically:
- Maintain a bounded local cache.
- Listen on
notify_channelso updates/deletes on any node evict stale entries everywhere. - Use advisory locks on
get_or_setto avoid thundering herds.
Null cache
Need a drop-in implementation that always hits the loader (for local dev or tests)?
from postgres_cache import NullCache
async with NullCache() as cache:
await cache.set("noop", "value") # does nothing
assert await cache.get("noop") is None
Load harness & schema
- See
harness/README.mdfor the load-test instructions andmake harness-load-testhelper. - Schema details (tables/triggers/functions) live in the same document so the operational guidance stays in one place.
License
Released under the MIT License.
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 postgres_cache-0.1.0.tar.gz.
File metadata
- Download URL: postgres_cache-0.1.0.tar.gz
- Upload date:
- Size: 13.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
810ae5973de277ed564ab81a79860c6848e2e01d7fd02b169fef70d23fd40fb6
|
|
| MD5 |
453d214402fda713cfbe6cb4171c2a56
|
|
| BLAKE2b-256 |
69f8d90f75927fa3d31f112ccac8b5c036148a5a10071cb4865feefd49ef4425
|
Provenance
The following attestation bundles were made for postgres_cache-0.1.0.tar.gz:
Publisher:
publish.yml on chaliy/postgres-cache
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
postgres_cache-0.1.0.tar.gz -
Subject digest:
810ae5973de277ed564ab81a79860c6848e2e01d7fd02b169fef70d23fd40fb6 - Sigstore transparency entry: 702292143
- Sigstore integration time:
-
Permalink:
chaliy/postgres-cache@63acd96b9840ec169b3f82ba452477f1d4352632 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/chaliy
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@63acd96b9840ec169b3f82ba452477f1d4352632 -
Trigger Event:
release
-
Statement type:
File details
Details for the file postgres_cache-0.1.0-py3-none-any.whl.
File metadata
- Download URL: postgres_cache-0.1.0-py3-none-any.whl
- Upload date:
- Size: 13.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8ae5f477ae98e6d26f4ed38712b0d4dd0b6c0594cbf3f49f58a7e6a386bb3a09
|
|
| MD5 |
c9e3d764cd9e17b508f70d48503c1548
|
|
| BLAKE2b-256 |
caeb7bfb2814499a7ea4c1695c55b9eb42a9a76358018fe29be927b6729776e6
|
Provenance
The following attestation bundles were made for postgres_cache-0.1.0-py3-none-any.whl:
Publisher:
publish.yml on chaliy/postgres-cache
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
postgres_cache-0.1.0-py3-none-any.whl -
Subject digest:
8ae5f477ae98e6d26f4ed38712b0d4dd0b6c0594cbf3f49f58a7e6a386bb3a09 - Sigstore transparency entry: 702292144
- Sigstore integration time:
-
Permalink:
chaliy/postgres-cache@63acd96b9840ec169b3f82ba452477f1d4352632 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/chaliy
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@63acd96b9840ec169b3f82ba452477f1d4352632 -
Trigger Event:
release
-
Statement type: