cache

package
v0.0.0-...-244743e Latest Latest
Warning

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

Go to latest
Published: Feb 1, 2016 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// Policy is one of the consts listed for EvictionPolicy.
	Policy EvictionPolicy

	// ShouldEvict is a callback function executed each time a new entry
	// is added to the cache. It supplies cache size, and potential
	// evictee's key and value. The function should return true if the
	// entry may be evicted; false otherwise. For example, to support a
	// maximum size for the cache, use a method like:
	//
	//   func(size int, key Key, value interface{}) { return size > maxSize }
	//
	// To support max TTL in the cache, use something like:
	//
	//   func(size int, key Key, value interface{}) {
	//     return time.Now().UnixNano() - value.(int64) > maxTTLNanos
	//   }
	ShouldEvict func(size int, key, value interface{}) bool

	// OnEvicted optionally specifies a callback function to be
	// executed when an entry is purged from the cache.
	OnEvicted func(key, value interface{})
}

A Config specifies the eviction policy, eviction trigger callback, and eviction listener callback.

type EvictionPolicy

type EvictionPolicy int

EvictionPolicy is the cache eviction policy enum.

const (
	CacheLRU  EvictionPolicy = iota // Least recently used
	CacheFIFO                       // First in, first out
	CacheNone                       // No evictions; don't maintain ordering list
)

Constants describing LRU and FIFO, and None cache eviction policies respectively.

type IntervalCache

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

IntervalCache is a cache which supports querying of intervals which match a key or range of keys. It is backed by an interval tree. See comments in UnorderedCache for more details on cache functionality.

Note that the IntervalCache allow multiple identical segments, as specified by start and end keys.

Keys supplied to the IntervalCache's Get, Add & Del methods must be constructed from IntervalCache.NewKey().

IntervalCache is not safe for concurrent access.

func NewIntervalCache

func NewIntervalCache(config Config) *IntervalCache

NewIntervalCache creates a new Cache backed by an interval tree. See NewCache() for details on parameters.

func (IntervalCache) Add

func (bc IntervalCache) Add(key, value interface{})

Add adds a value to the cache.

func (IntervalCache) Clear

func (bc IntervalCache) Clear()

Clear clears all entries from the cache.

func (IntervalCache) Del

func (bc IntervalCache) Del(key interface{})

Del removes the provided key from the cache.

func (*IntervalCache) Do

func (ic *IntervalCache) Do(f func(k, v interface{}))

Do invokes f on all of the entries in the cache.

func (IntervalCache) Get

func (bc IntervalCache) Get(key interface{}) (value interface{}, ok bool)

Get looks up a key's value from the cache.

func (*IntervalCache) GetOverlaps

func (ic *IntervalCache) GetOverlaps(start, end interval.Comparable) []Overlap

GetOverlaps returns a slice of values which overlap the specified interval.

func (IntervalCache) Len

func (bc IntervalCache) Len() int

Len returns the number of items in the cache.

func (*IntervalCache) NewKey

func (ic *IntervalCache) NewKey(start, end interval.Comparable) *IntervalKey

NewKey creates a new interval key defined by start and end values.

type IntervalKey

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

IntervalKey provides uniqueness as well as key interval.

func (*IntervalKey) Contains

func (ik *IntervalKey) Contains(lk *IntervalKey) bool

Contains returns true if the specified IntervalKey is contained within this IntervalKey.

func (*IntervalKey) End

func (ik *IntervalKey) End() interval.Comparable

End .

func (*IntervalKey) Overlap

func (ik *IntervalKey) Overlap(r interval.Range) bool

Overlap .

func (*IntervalKey) SetEnd

func (ik *IntervalKey) SetEnd(c interval.Comparable)

SetEnd .

func (*IntervalKey) SetStart

func (ik *IntervalKey) SetStart(c interval.Comparable)

SetStart .

func (*IntervalKey) Start

func (ik *IntervalKey) Start() interval.Comparable

Start .

func (IntervalKey) String

func (ik IntervalKey) String() string

type OrderedCache

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

OrderedCache is a cache which supports binary searches using Ceil and Floor methods. It is backed by a left-leaning red black tree. See comments in UnorderedCache for more details on cache functionality.

OrderedCache requires that keys implement llrb.Comparable.

OrderedCache is not safe for concurrent access.

func NewOrderedCache

func NewOrderedCache(config Config) *OrderedCache

NewOrderedCache creates a new Cache backed by a left-leaning red black binary tree which supports binary searches via the Ceil() and Floor() methods. See NewUnorderedCache() for details on parameters.

func (OrderedCache) Add

func (bc OrderedCache) Add(key, value interface{})

Add adds a value to the cache.

func (*OrderedCache) Ceil

func (oc *OrderedCache) Ceil(key interface{}) (interface{}, interface{}, bool)

Ceil returns the smallest cache entry greater than or equal to key.

func (OrderedCache) Clear

func (bc OrderedCache) Clear()

Clear clears all entries from the cache.

func (OrderedCache) Del

func (bc OrderedCache) Del(key interface{})

Del removes the provided key from the cache.

func (*OrderedCache) Do

func (oc *OrderedCache) Do(f func(k, v interface{}))

Do invokes f on all of the entries in the cache.

func (*OrderedCache) DoRange

func (oc *OrderedCache) DoRange(f func(k, v interface{}), from, to interface{})

DoRange invokes f on all cache entries in the range of from -> to.

func (*OrderedCache) Floor

func (oc *OrderedCache) Floor(key interface{}) (interface{}, interface{}, bool)

Floor returns the greatest cache entry less than or equal to key.

func (OrderedCache) Get

func (bc OrderedCache) Get(key interface{}) (value interface{}, ok bool)

Get looks up a key's value from the cache.

func (OrderedCache) Len

func (bc OrderedCache) Len() int

Len returns the number of items in the cache.

type Overlap

type Overlap struct {
	Key   *IntervalKey
	Value interface{}
}

Overlap contains the key/value pair for one overlap instance.

type UnorderedCache

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

UnorderedCache is a cache which supports custom eviction triggers and two eviction policies: LRU and FIFO. A listener pattern is available for eviction events. This cache uses a hashmap for storing elements, making it the most performant. Only exact lookups are supported.

UnorderedCache requires that keys are comparable, according to the go specification (http://golang.org/ref/spec#Comparison_operators).

UnorderedCache is not safe for concurrent access.

func NewUnorderedCache

func NewUnorderedCache(config Config) *UnorderedCache

NewUnorderedCache creates a new UnorderedCache backed by a hash map.

func (UnorderedCache) Add

func (bc UnorderedCache) Add(key, value interface{})

Add adds a value to the cache.

func (UnorderedCache) Clear

func (bc UnorderedCache) Clear()

Clear clears all entries from the cache.

func (UnorderedCache) Del

func (bc UnorderedCache) Del(key interface{})

Del removes the provided key from the cache.

func (UnorderedCache) Get

func (bc UnorderedCache) Get(key interface{}) (value interface{}, ok bool)

Get looks up a key's value from the cache.

func (UnorderedCache) Len

func (bc UnorderedCache) Len() int

Len returns the number of items in the cache.

Jump to

Keyboard shortcuts

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