Documentation ¶
Index ¶
- type Config
- type Entry
- type EvictionPolicy
- type IntervalCache
- func (bc *IntervalCache) Add(key, value interface{})
- func (bc *IntervalCache) AddEntry(entry *Entry)
- func (bc *IntervalCache) Clear()
- func (bc *IntervalCache) Del(key interface{})
- func (ic *IntervalCache) Do(f func(k, v interface{}))
- func (bc *IntervalCache) Get(key interface{}) (value interface{}, ok bool)
- func (ic *IntervalCache) GetOverlaps(start, end []byte) []Overlap
- func (bc *IntervalCache) Len() int
- func (ic *IntervalCache) MakeKey(start, end []byte) IntervalKey
- func (ic *IntervalCache) NewKey(start, end []byte) *IntervalKey
- type IntervalKey
- type OrderedCache
- func (bc *OrderedCache) Add(key, value interface{})
- func (bc *OrderedCache) AddEntry(entry *Entry)
- func (oc *OrderedCache) Ceil(key interface{}) (interface{}, interface{}, bool)
- func (bc *OrderedCache) Clear()
- func (bc *OrderedCache) Del(key interface{})
- func (oc *OrderedCache) Do(f func(k, v interface{}))
- func (oc *OrderedCache) DoRange(f func(k, v interface{}), from, to interface{})
- func (oc *OrderedCache) Floor(key interface{}) (interface{}, interface{}, bool)
- func (bc *OrderedCache) Get(key interface{}) (value interface{}, ok bool)
- func (bc *OrderedCache) Len() int
- type Overlap
- type UnorderedCache
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 Entry ¶
type Entry struct {
Key, Value interface{}
// contains filtered or unexported fields
}
Entry holds the key and value and a pointer to the linked list which defines the eviction ordering.
func (*Entry) Compare ¶
func (e *Entry) Compare(b llrb.Comparable) int
Compare implements the llrb.Comparable interface for cache entries. This facility is used by the OrderedCache, and crucially requires that keys used with that cache implement llrb.Comparable.
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) AddEntry ¶
func (bc *IntervalCache) AddEntry(entry *Entry)
AddEntry adds a value to the cache. It provides an alternative interface to Add which the caller can use to reduce allocations by bundling the Entry structure with the key and value to be stored.
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 []byte) []Overlap
GetOverlaps returns a slice of values which overlap the specified interval. The slice is only valid until the next call to GetOverlaps.
func (*IntervalCache) Len ¶
func (bc *IntervalCache) Len() int
Len returns the number of items in the cache.
func (*IntervalCache) MakeKey ¶
func (ic *IntervalCache) MakeKey(start, end []byte) IntervalKey
MakeKey creates a new interval key defined by start and end values.
func (*IntervalCache) NewKey ¶
func (ic *IntervalCache) NewKey(start, end []byte) *IntervalKey
NewKey creates a new interval key defined by start and end values.
type IntervalKey ¶
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) 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) AddEntry ¶
func (bc *OrderedCache) AddEntry(entry *Entry)
AddEntry adds a value to the cache. It provides an alternative interface to Add which the caller can use to reduce allocations by bundling the Entry structure with the key and value to be stored.
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.
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) AddEntry ¶
func (bc *UnorderedCache) AddEntry(entry *Entry)
AddEntry adds a value to the cache. It provides an alternative interface to Add which the caller can use to reduce allocations by bundling the Entry structure with the key and value to be stored.
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.