memo overview
Configure an in-memory Go cache with eviction, jittered expiration, batch loading, and background refresh.
Use memo to cache database queries or other reloadable data within a process. New validates the Config and creates a concurrency-safe Cache. Capacity is required. Other features are enabled through configuration.
Installation
You need Go 1.27 or later. Run the installation command in your Go module.
go get github.com/luminyra/memoCapacity, loading, and expiration
- Choose
LRU,LFU, orFIFOto decide which entry to evict when the cache reaches capacity. - Read one key with
Cache.Get. UseCache.GetManyto combine multiple misses into oneLoadercall. - Add jitter to spread expiration times. Enable background refresh to serve stale values briefly while reloading them asynchronously.
Go standard library integration
memo validates configuration once, loads values in batches, and exposes a Go iterator through Cache.All. It uses the standard library for time, so tests can control time with testing/synctest.
Performance
A cache hit takes about 16 ns. A write that triggers eviction allocates no memory and takes about 43 ns with LRU or FIFO and 109 ns with LFU. GetMany takes about 1.7 µs for 64 cached keys. For concurrent workloads, use Config.Shards to split the cache across independently locked shards. See the eviction guide.
The quickstart walks through cache hits, calls to the loader, and both read methods.