cache-pg
Postgres and SQLite backend for cache over database/sql, with Migrate and Vacuum.
go get github.com/ubgo/cache-pg
- Language
- Go
- Go version
- 1.24
- Core deps
- 2 direct
- Latest tag
- v0.1.0
- License
- Apache-2.0
- Go modules
- 1
Highlights from the README
- No new infrastructure. If you run Postgres, you already have the cache store. One table, two indexes,
Migrate()creates them. - Durable. Cached entries survive process restarts (unlike an in-memory cache) — useful for expensive-to-recompute values and idempotency keys.
- Driver-agnostic. Takes a
*sql.DB. Bringpgx,lib/pq, or — for tests and embedded use — SQLite. The conformance suite runs in-process on SQLite with no Docker. - Codegen-free. Plain
database/sql, no ORM, no generated client. Easy to audit, easy to vendor. - Atomic where it matters.
SetNX,Incr,Decr, andSetMultirun inside transactions so concurrent callers stay correct.
Code from the README
package main
import (
"context"
"database/sql"
"log"
"time"
_ "github.com/jackc/pgx/v5/stdlib"
pgcache "github.com/ubgo/cache-pg"
)
func main() {
db, err := sql.Open("pgx", "postgres://localhost:5432/app?sslmode=disable")
if err != nil {
log.Fatal(err)
}
c := pgcache.New(db) // Postgres dialect by default
if err := c.Migrate(context.Background()); err != nil { // idempotent
log.Fatal(err)
}
defer c.Close()
ctx := context.Background()
_ = c.Set(ctx, "report:q3", []byte("…"), time.Hour)
// Reclaim physically-dead rows on a ticker / cron:
_ = c.Vacuum(ctx)
}go func() {
t := time.NewTicker(10 * time.Minute)
for range t.C {
_ = c.Vacuum(context.Background())
}
}()From the cache-pg README.
At a glance (from the README)
| Adapter | Backing store | Durable | TTL introspection | Prefix delete | Best for |
|---|---|---|---|---|---|
| cache-pg (this) | Postgres / SQLite | Yes | Yes | Yes (LIKE) | Durable cache where you already run Postgres |
| cache-redis | Redis 6+ | configurable | Yes | Yes (SCAN) | Shared distributed cache, low latency |
| cache-tiered | composes others | via tiers | via tiers | via tiers | L1/L2 hot-path latency |
| cache-memcached | Memcached | No | No (unsupported) | No (unsupported) | Drop-in Memcached interop |