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/v2does the heavy lifting.) - TTL. Entries older than the TTL miss on
Getand are evicted as a side effect of being touched. Set TTL to0to disable expiry and use the cache as pure LRU. - Lock everything. All operations take the same
sync.Mutex— hashicorp’s LRU mutates internal state onGet(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)
}threadsafecache.New[int](
1024, ttl,
threadsafecache.WithClock[int](myFakeClock), // tests only
)From the threadsafecache README.