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

Skip to content
ubgo Alpha

threadsafecache deprecated

Generic thread-safe LRU and TTL cache. Deprecated; use cache-obj instead.

go get github.com/ubgo/threadsafecache

Deprecated. Use cache-obj instead.

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

  • Generics. Values are stored without boxing through interface{}.
  • LRU bound. When the cache fills, the least-recently-used entry is evicted. (hashicorp/golang-lru/v2 does the heavy lifting.)
  • TTL. Entries older than the TTL miss on Get and are evicted as a side effect of being touched. Set TTL to 0 to disable expiry and use the cache as pure LRU.
  • Lock everything. All operations take the same sync.Mutex — hashicorp’s LRU mutates internal state on Get (recency tracking), so splitting reads and writes is unsafe in general. The result is simple and race-clean.

Code from the README

import "github.com/ubgo/threadsafecache"

c, _ := threadsafecache.New[string](
    1024,                     // LRU size
    5 * time.Minute,          // TTL (0 = disabled)
)

c.Set("user:42", payload)

if v, ok := c.Get("user:42"); ok {
    use(v)
}

From the threadsafecache README.