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

Skip to content
ubgo Alpha

cache-mem

Sharded in-memory backend for cache with W-TinyLFU or LRU eviction and optional snapshots.

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

Highlights from the README

  • No network, no serialization tax. Values live in process memory; reads are a sharded map lookup plus a defensive copy.
  • High hit rates out of the box. The default policy is Adaptive W-TinyLFU, which resists one-hit-wonder floods and scan-heavy access patterns far better than plain LRU.
  • Low lock contention. Keys are hashed across N independent shards, each with its own mutex and its own policy state.
  • Survives restarts when you want it to. Choose periodic checkpoints (cheap, lossy to the last checkpoint) or AOF (fsync per write, near-zero loss), or both.
  • Size-aware. Cap by entry count, by weighed byte size, or both, with a custom weigher.
  • Conformance-tested. The same suite that validates every ubgo cache backend validates this one.

Code from the README

package main

import (
	"context"
	"fmt"
	"time"

	memcache "github.com/ubgo/cache-mem"
)

func main() {
	c := memcache.New(
		memcache.WithMaxEntries(100_000),
		memcache.WithMaxBytes(256<<20),
	)
	defer c.Close()

	ctx := context.Background()
	_ = c.Set(ctx, "user:1", []byte("alice"), 5*time.Minute)

	v, err := c.Get(ctx, "user:1")
	if err != nil {
		panic(err)
	}
	fmt.Println(string(v)) // alice
}

From the cache-mem README.