cache

package
v0.0.0-...-6602e3a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 19, 2018 License: GPL-3.0 Imports: 6 Imported by: 4

Documentation

Index

Constants

View Source
const ErrTimeout = err("timeout")

ErrTimeout is the value returned by Gets to cache with a given timeout

View Source
const ErrWrongMFetchResult = err("MFetchFunc error: keySuffixes and values returned doesn't have the same length")

ErrWrongMFetchResult happens when a MFetchFunc doesn't return results for every value requested

Variables

This section is empty.

Functions

This section is empty.

Types

type FetchFunc

type FetchFunc func() (value interface{}, err error)

FetchFunc result is stored in cache, if value is nil, it will be considered as empty and an empty item will be stored. When err is non-nil, the cache won't store anything and the error will be scalated to the function calling fetch

type FetcherLRU

type FetcherLRU struct {
	// contains filtered or unexported fields
}

FetcherLRU wraps https://github.com/hashicorp/golang-lru providing ttl, stats and singleflight

Satisfies an use case where the cache is filled by demand: the gets are done by key and an OnFetch func that is executed when the key is not found or has expired, the OnFetch Result will be stored in cache for that key. Only one call to OnFetch is done at the same time

Gets on missing keys are blocked until the OnFetch finishes

func New

func New(size int, ttl time.Duration, options ...FetcherLRUOption) (*FetcherLRU, error)

New creates a new instance of FetcherLRU

func (*FetcherLRU) Add

func (c *FetcherLRU) Add(key string, value interface{})

Add forces an addition for a key's value

func (*FetcherLRU) GetChan

func (c *FetcherLRU) GetChan(key string, onFetch FetchFunc) <-chan Result

GetChan is the same as GetOrFetch but returning a channel with the result when its ready

func (*FetcherLRU) GetOrFetch

func (c *FetcherLRU) GetOrFetch(key string, onFetch FetchFunc) (interface{}, error)

GetOrFetch gets the cache's value for key or the onFetch's value result

func (*FetcherLRU) GetWithTimeout

func (c *FetcherLRU) GetWithTimeout(key string, onFetch FetchFunc, timeout time.Duration) (interface{}, error)

GetWithTimeout is the same as GetOrFetch but returns a timeout error if the result is not ready on a given duration, the FetchFunc won't be cancelled

func (*FetcherLRU) MGetOrFetch

func (c *FetcherLRU) MGetOrFetch(keyPrefix string, keySuffixes []string, onFetch MFetchFunc) ([]interface{}, error)

MGetOrFetch gets the cache's values for keys or onFetch's values result, repeated keys are handled to only be called once to the onFetch func

func (*FetcherLRU) Remove

func (c *FetcherLRU) Remove(key string)

func (*FetcherLRU) Stats

func (c *FetcherLRU) Stats() StatsFetcherLRU

Stats returns a snapshot of the current cache's Stats

type FetcherLRUOption

type FetcherLRUOption func(*FetcherLRUOptions)

FetcherLRUOption is a function that sets some option on the FetcherLRU.

func SetBlockOnUpdatingGoroutine

func SetBlockOnUpdatingGoroutine() FetcherLRUOption

SetBlockOnUpdatingGoroutine makes the calling goroutine to block until the new value is fetched

func SetEvictFunc

func SetEvictFunc(evictFunc func(key interface{}, value interface{})) FetcherLRUOption

SetEvictFunc sets a callback func executed on an item eviction

type FetcherLRUOptions

type FetcherLRUOptions struct {
	// contains filtered or unexported fields
}

FetcherLRUOptions are optional paratemer for a customized FetcherLRU instance

type MFetchFunc

type MFetchFunc func(keyPrefix string, keySuffixes []string) (values []interface{}, err error)

MFetchFunc expects the values returned to be in the same order as the keySuffixes requested

type Result

type Result struct {
	Val interface{}
	Err error
}

Result holds the results of Get, so they can be passed on a channel.

type StatsFetcherLRU

type StatsFetcherLRU struct {
	Hits      int64
	Misses    int64
	Evictions int64
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL