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

Skip to content
ubgo Alpha

cache-obj

In-process cache that holds live Go objects by reference, with TTL and LRU and no serialization.

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

How it fits together

passes objtest
cache-objcore · 2 direct depsPACKAGESobjtestobjtest

objtest is the shared conformance suite: each one marked green has a test that runs it.

Highlights from the README

  • Live objects, by reference. Get returns the same instance you Set — no serialization, no copy. The only cache in the family that can hold non-serializable values.
  • Generics. Cache[T] stores T directly, no interface{} boxing.
  • Per-entry TTL. SetTTL(key, v, ttl); ttl <= 0 means the entry never expires. WithDefaultTTL sets the TTL Set applies.
  • LRU bound. WithCapacity(n) evicts the least-recently-used entry when full. Omit it for an unbounded cache.
  • Value-bearing eviction hook. WithOnEvict(func(key string, v T, cause cache.EvictionCause)) fires when an entry is dropped involuntarily (capacity or expiry) and hands you the value — so you can close handles or release resources.
  • Single-flight Remember. Get-or-load that collapses a thundering herd: N concurrent misses for the same key run the loader once and share the result.
  • Optional background sweeper. WithSweepInterval(d) proactively evicts expired entries; Close() stops it. Default expiry is lazy (no goroutine).
  • Stats. Stats() returns the shared cache.Stats shape, so observability reads identically across the family.

Code from the README

import cacheobj "github.com/ubgo/cache-obj"

// Cache compiled regexes — a value that must stay live (it can't survive a codec).
re := cacheobj.New[*regexp.Regexp](cacheobj.WithCapacity(128))

re.Set(`\d+`, regexp.MustCompile(`\d+`))

if r, ok := re.Get(`\d+`); ok {
    r.MatchString("abc123") // same compiled program, zero cost
}

From the cache-obj README.