| 
				
					 | 
			4 年之前 | |
|---|---|---|
| .. | ||
| LICENSE | 4 年之前 | |
| README.md | 4 年之前 | |
| cache.go | 4 年之前 | |
| filter.go | 4 年之前 | |
| go.mod | 4 年之前 | |
| hash.go | 4 年之前 | |
| local.go | 4 年之前 | |
| lru.go | 4 年之前 | |
| policy.go | 4 年之前 | |
| sketch.go | 4 年之前 | |
| stats.go | 4 年之前 | |
| tinylfu.go | 4 年之前 | |
Partial implementations of Guava Cache in Go.
Supported cache replacement policies:
The TinyLFU implementation is inspired by Caffeine by Ben Manes and go-tinylfu by Damian Gryski.
go get -u github.com/goburrow/cache
package main
import (
	"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)
		}
	}
}