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
}c := memcache.New(
memcache.WithShards(32),
memcache.WithMaxEntries(1_000_000),
memcache.WithMaxBytes(512<<20),
memcache.WithPolicy(memcache.AdaptiveWTinyLFU),
)
defer c.Close()c := memcache.New(
memcache.WithMaxBytes(64<<20),
memcache.WithWeigher(func(v []byte) int64 { return int64(len(v)) + 48 }),
)type Policy int
const (
LRU Policy = iota
AdaptiveWTinyLFU
)From the cache-mem README.