https://github.com/goburrow/cache
Mango Cache 🥠- Partial implementation of Guava Cache in Go (golang).
https://github.com/goburrow/cache
cache golang lru slru tinylfu
Last synced: 4 days ago
JSON representation
Mango Cache 🥠- Partial implementation of Guava Cache in Go (golang).
- Host: GitHub
- URL: https://github.com/goburrow/cache
- Owner: goburrow
- License: bsd-3-clause
- Created: 2016-10-23T12:13:03.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2022-10-14T19:33:07.000Z (over 2 years ago)
- Last Synced: 2025-04-07T18:09:39.973Z (about 1 month ago)
- Topics: cache, golang, lru, slru, tinylfu
- Language: Go
- Homepage:
- Size: 196 KB
- Stars: 594
- Watchers: 12
- Forks: 46
- Open Issues: 10
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Mango Cache
[](https://godoc.org/github.com/goburrow/cache)
Partial implementations of [Guava Cache](https://github.com/google/guava) in Go.
Supported cache replacement policies:
- LRU
- Segmented LRU (default)
- TinyLFU (experimental)The TinyLFU implementation is inspired by
[Caffeine](https://github.com/ben-manes/caffeine) by Ben Manes and
[go-tinylfu](https://github.com/dgryski/go-tinylfu) by Damian Gryski.## Download
```
go get -u github.com/goburrow/cache
```## Example
```go
package mainimport (
"fmt"
"math/rand"
"time""github.com/goburrow/cache"
)func main() {
load := func(k cache.Key) (cache.Value, error) {
time.Sleep(100 * time.Millisecond) // Slow task
return fmt.Sprintf("%d", k), nil
}
// Create a loading cache
c := cache.NewLoadingCache(load,
cache.WithMaximumSize(100), // Limit number of entries in the cache.
cache.WithExpireAfterAccess(1*time.Minute), // Expire entries after 1 minute since last accessed.
cache.WithRefreshAfterWrite(2*time.Minute), // Expire entries after 2 minutes since last created.
)getTicker := time.Tick(100 * time.Millisecond)
reportTicker := time.Tick(5 * time.Second)
for {
select {
case <-getTicker:
_, _ = c.Get(rand.Intn(200))
case <-reportTicker:
st := cache.Stats{}
c.Stats(&st)
fmt.Printf("%+v\n", st)
}
}
}
```## Performance
See [traces](traces/) and [benchmark](https://github.com/goburrow/cache/wiki/Benchmark)
