https://github.com/bep/lazycache
Thread safe in-memory LRU cache with non-blocking cache priming on cache misses.
https://github.com/bep/lazycache
cache go-library lru-cache
Last synced: 4 months ago
JSON representation
Thread safe in-memory LRU cache with non-blocking cache priming on cache misses.
- Host: GitHub
- URL: https://github.com/bep/lazycache
- Owner: bep
- License: mit
- Created: 2022-11-07T15:18:07.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2026-01-10T20:24:58.000Z (4 months ago)
- Last Synced: 2026-01-11T06:13:33.564Z (4 months ago)
- Topics: cache, go-library, lru-cache
- Language: Go
- Homepage:
- Size: 44.9 KB
- Stars: 23
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
[](https://github.com/bep/lazycache/actions?query=workflow:Test)
[](https://goreportcard.com/report/github.com/bep/lazycache)
[](https://codecov.io/github/bep/lazycache)
[](https://godoc.org/github.com/bep/lazycache)
**Lazycache** is a simple thread safe in-memory LRU cache. Under the hood it leverages the great [simpleru package in golang-lru](https://github.com/hashicorp/golang-lru), with its exellent performance. One big difference between `golang-lru` and this library is the [GetOrCreate](https://pkg.go.dev/github.com/bep/lazycache#Cache.GetOrCreate) method, which provides:
* Non-blocking cache priming on cache misses.
* A guarantee that the prime function is only called once for a given key.
* The cache's [RWMutex](https://pkg.go.dev/sync#RWMutex) is not locked during the execution of the prime function, which should make it easier to reason about potential deadlocks.
Other notable features:
* The API is [generic](https://go.dev/doc/tutorial/generics)
* The cache can be [resized](https://pkg.go.dev/github.com/bep/lazycache#Cache.Resize) while running.
* When the number of entries overflows the defined cache size, the least recently used item gets discarded (LRU).