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

Skip to content
ubgo Alpha

cache-redis

Redis 6+ backend for cache with prefix isolation and Pub/Sub invalidation across processes.

go get github.com/ubgo/cache-redis
Language
Go
Go version
1.24
Core deps
3 direct
Latest tag
v0.1.0
License
Apache-2.0
Go modules
1

Highlights from the README

  • Native Redis, no reinvention. Every operation maps to the right Redis command: SET EX/PX, SET NX, INCRBY/DECRBY, MGET, pipelined multi-set. No client-side TTL bookkeeping.
  • Safe on huge keyspaces. DeleteByPrefix and Iterate use cursor-based SCAN with batched DEL — never the blocking KEYS command — so they are safe to run against a multi-million-key production database.
  • Multi-tenant safe. WithPrefix namespaces every key, and Flush is scoped to that prefix so two services can share one Redis database without stepping on each other.
  • Tiered-cache ready. NewInvalidation ships a Redis Pub/Sub invalidation bus so an L1/L2 cache-tiered deployment can run a long L1 TTL and still stay coherent across pods.
  • Conformance-tested, Docker-free. Passes the shared cachetest.Run suite fully in-process via miniredis. No Redis service required in CI.

Code from the README

package main

import (
	"context"
	"log"
	"time"

	"github.com/redis/go-redis/v9"
	rediscache "github.com/ubgo/cache-redis"
)

func main() {
	rdb := redis.NewClient(&redis.Options{Addr: "localhost:6379"})

	c := rediscache.New(rdb, rediscache.WithPrefix("svc:billing"))
	defer c.Close()

	ctx := context.Background()

	if err := c.Set(ctx, "invoice:42", []byte(`{"total":1999}`), time.Minute); err != nil {
		log.Fatal(err)
	}

	b, err := c.Get(ctx, "invoice:42") // (nil, cache.ErrNotFound) on miss
	if err != nil {
		log.Fatal(err)
	}
	log.Printf("%s", b)
}

From the cache-redis README.

At a glance (from the README)

AdapterBacking storeTTL introspectionPrefix opsCross-process invalidationBest for
cache-redis (this)Redis 6+Yes (TTL)Yes (SCAN)Yes (Pub/Sub)Shared distributed cache, L2 tier
cache-pgPostgres / SQLiteYesYes (LIKE)NoDurable cache where you already run Postgres
cache-tieredcomposes othersvia tiersvia tiersYes (pluggable bus)L1/L2 hot-path latency
cache-memcachedMemcachedNo (unsupported)No (unsupported)NoDrop-in interop with an existing Memcached fleet