Categorygithub.com/goburrow/cache
modulepackage
0.1.4
Repository: https://github.com/goburrow/cache.git
Documentation: pkg.go.dev

# README

Mango Cache

GoDoc Go

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) {
		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 and benchmark

report

# Packages

No description provided by the author
No description provided by the author
No description provided by the author

# Functions

New returns a local in-memory Cache.
NewLoadingCache returns a new LoadingCache with given loader function and cache options.
WithExpireAfterAccess returns an option to expire a cache entry after the given duration without being accessed.
WithExpireAfterWrite returns an option to expire a cache entry after the given duration from creation.
WithMaximumSize returns an Option which sets maximum size for the cache.
WithPolicy returns an option which sets cache policy associated to the given name.
WithRefreshAfterWrite returns an option to refresh a cache entry after the given duration.
WithReloader returns an option which sets reloader for a loading cache.
WithRemovalListener returns an Option to set cache to call onRemoval for each entry evicted from the cache.
WithStatsCounter returns an option which overrides default cache stats counter.

# Structs

Stats is statistics about performance of a cache.

# Interfaces

Cache is a key-value cache which entries are added and stayed in the cache until either are evicted or manually invalidated.
Hash is an interface implemented by cache keys to override default hash function.
Key is any value which is comparable.
LoadingCache is a cache with values are loaded automatically and stored in the cache until either evicted or manually invalidated.
Reloader specifies how cache loader is run to refresh value for the given Key.
StatsCounter accumulates statistics of a cache.
Value is any value.

# Type aliases

Func is a generic callback for entry events in the cache.
LoaderFunc retrieves the value corresponding to given Key.
Option add options for default Cache.