Mango Cache - Partial implementations of Guava Cache in Go.
Go Shell
Switch branches/tags
Nothing to show
Clone or download
Latest commit 576a8d1 Apr 10, 2018

README.md

Mango Cache

GoDoc Build Status

Partial implementations of Guava Cache in Go.

Supported cache replacement policies:

  • LRU
  • Segmented LRU (default)
  • TinyLFU (experimental)

The TinyLFU implementation is inspired by Caffeine by Ben Manes and go-tinylfu by Damian Gryski.

Download

go get -u github.com/goburrow/cache

Example

package main

import (
	"fmt"
	"math/rand"
	"time"

	"github.com/goburrow/cache"
)

func main() {
	load := func(k cache.Key) (cache.Value, error) {
		return fmt.Sprintf("%d", k), nil
	}
	// Create a new cache
	c := cache.NewLoadingCache(load,
		cache.WithMaximumSize(1000),
		cache.WithExpireAfterAccess(10*time.Second),
		cache.WithRefreshAfterWrite(60*time.Second),
	)

	getTicker := time.Tick(10 * time.Millisecond)
	reportTicker := time.Tick(1 * time.Second)
	for {
		select {
		case <-getTicker:
			_, _ = c.Get(rand.Intn(2000))
		case <-reportTicker:
			st := cache.Stats{}
			c.Stats(&st)
			fmt.Printf("%+v\n", st)
		}
	}
}

Performance

See traces

report