ubgo is in early alpha: libraries and APIs may still change.

Skip to content
ubgo Alpha

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. Bring pgx, 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, and SetMulti run 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)
}

From the cache-pg README.

At a glance (from the README)

AdapterBacking storeDurableTTL introspectionPrefix deleteBest for
cache-pg (this)Postgres / SQLiteYesYesYes (LIKE)Durable cache where you already run Postgres
cache-redisRedis 6+configurableYesYes (SCAN)Shared distributed cache, low latency
cache-tieredcomposes othersvia tiersvia tiersvia tiersL1/L2 hot-path latency
cache-memcachedMemcachedNoNo (unsupported)No (unsupported)Drop-in Memcached interop