Documentation ¶
Overview ¶
Package cache provides caching primitives for in-memory caches, including LRU eviction.
Index ¶
- type Cache
- type LRU
- type LRUHeap
- func (lrh *LRUHeap) Consume(consumer func(value *Value) bool)
- func (lrh *LRUHeap) Fix(newValue *Value)
- func (lrh *LRUHeap) Len() int
- func (lrh *LRUHeap) Peek() *Value
- func (lrh *LRUHeap) Pop() *Value
- func (lrh *LRUHeap) Push(object *Value)
- func (lrh *LRUHeap) Remove(key interface{})
- func (lrh *LRUHeap) Reset()
- type LRUHeapValues
- type LRUQueue
- func (lru *LRUQueue) Capacity() int
- func (lru *LRUQueue) Clear()
- func (lru *LRUQueue) Consume(consumer func(*Value) bool)
- func (lru *LRUQueue) Each(consumer func(*Value) bool)
- func (lru *LRUQueue) Fix(value *Value)
- func (lru *LRUQueue) Len() (len int)
- func (lru *LRUQueue) Peek() *Value
- func (lru *LRUQueue) PeekBack() *Value
- func (lru *LRUQueue) Pop() *Value
- func (lru *LRUQueue) Push(object *Value)
- func (lru *LRUQueue) Remove(key interface{})
- func (lru *LRUQueue) Reset()
- func (lru *LRUQueue) TrimExcess()
- type LocalCache
- func (lc *LocalCache) Get(key interface{}) (value interface{}, hit bool)
- func (lc *LocalCache) GetOrSet(key interface{}, valueProvider func() (interface{}, error), ...) (value interface{}, hit bool, err error)
- func (lc *LocalCache) Has(key interface{}) (has bool)
- func (lc *LocalCache) NotifyStarted() <-chan struct{}
- func (lc *LocalCache) NotifyStopped() <-chan struct{}
- func (lc *LocalCache) Remove(key interface{}) (value interface{}, hit bool)
- func (lc *LocalCache) Reset()
- func (lc *LocalCache) Set(key, value interface{}, options ...ValueOption)
- func (lc *LocalCache) Start() error
- func (lc *LocalCache) Stats() (stats Stats)
- func (lc *LocalCache) Stop() error
- func (lc *LocalCache) Sweep(ctx context.Context) error
- type LocalCacheOption
- type Locker
- type RemovalReason
- type Stats
- type Value
- type ValueOption
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache interface { Has(key interface{}) bool GetOrSet(key interface{}, valueProvider func() (interface{}, error), options ...ValueOption) (interface{}, bool, error) Set(key, value interface{}, options ...ValueOption) Get(key interface{}) (interface{}, bool) Remove(key interface{}) (interface{}, bool) }
Cache is a type that implements the cache interface.
type LRU ¶
type LRU interface { // Len returns the number of items in the queue. Len() int // Push should add a new value. The new minimum value should be returned by `Peek()` and `Pop()`. Push(*Value) // Pop should remove and return the minimum value, reordering the heap // to set a new minimum value. Pop() *Value // Peek should return (but not remove) the minimum value. Peek() *Value // Fix should update the LRU, replacing any existing values, reordering the heap. Fix(*Value) // Remove should remove a value with a given key, compacting the heap. Remove(interface{}) // Consume should iterate through the values. If `true` is removed by the handler, // the current value will be removed and the handler will be called on the next value. Consume(func(*Value) bool) // Reset should remove all values from the LRU, leaving an empty LRU. Reset() }
LRU is a type that implements the LRU methods.
type LRUHeap ¶
type LRUHeap struct {
Values LRUHeapValues
}
LRUHeap is a fifo buffer that is backed by a pre-allocated array, instead of allocating a whole new node object for each element (which saves GC churn). Enqueue can be O(n), Dequeue can be O(1).
func (*LRUHeap) Consume ¶
Consume calls the consumer for each element in the buffer, while also dequeueing that entry. The consumer should return `true` if it should remove the item and continue processing. If `false` is returned, the current item will be left in place.
func (*LRUHeap) Len ¶
Len returns the length of the queue (as it is currently populated). Actual memory footprint may be different.
type LRUHeapValues ¶
type LRUHeapValues []*Value
LRUHeapValues is an alias that allows for use of a Value array as a heap storage backend. It also implements sorting for other use cases.
func (LRUHeapValues) Less ¶
func (lruv LRUHeapValues) Less(i, j int) bool
Less returns if two values are strictly less than eachother.
func (LRUHeapValues) Swap ¶
func (lruv LRUHeapValues) Swap(i, j int)
Swap swaps values at the given positions.
type LRUQueue ¶
type LRUQueue struct {
// contains filtered or unexported fields
}
LRUQueue is a fifo buffer that is backed by a pre-allocated array, instead of allocating a whole new node object for each element (which saves GC churn). Enqueue can be O(n), Dequeue can be O(1).
func (*LRUQueue) Capacity ¶
Capacity returns the total size of the ring bufffer, including empty elements.
func (*LRUQueue) Consume ¶
Consume calls the consumer for each element in the buffer. If the handler returns true, the element is popped and the handler is called on the next value.
func (*LRUQueue) Each ¶
Each iterates through the queue and calls the consumer for each element of the queue.
func (*LRUQueue) Len ¶
Len returns the length of the ring buffer (as it is currently populated). Actual memory footprint may be different.
func (*LRUQueue) Remove ¶
func (lru *LRUQueue) Remove(key interface{})
Remove removes an item from the queue by its key.
func (*LRUQueue) Reset ¶
func (lru *LRUQueue) Reset()
Reset removes all elements from the heap, leaving an empty heap.
func (*LRUQueue) TrimExcess ¶
func (lru *LRUQueue) TrimExcess()
TrimExcess trims the excess space in the ringbuffer.
type LocalCache ¶
LocalCache is a memory LocalCache.
func New ¶
func New(options ...LocalCacheOption) *LocalCache
New returns a new LocalLocalCache. It defaults to 500ms sweep intervals and an LRU queue for invalidation.
func (*LocalCache) Get ¶
func (lc *LocalCache) Get(key interface{}) (value interface{}, hit bool)
Get gets a value based on a key.
func (*LocalCache) GetOrSet ¶
func (lc *LocalCache) GetOrSet(key interface{}, valueProvider func() (interface{}, error), options ...ValueOption) (value interface{}, hit bool, err error)
GetOrSet gets a value by a key, and in the case of a miss, sets the value from a given value provider lazily. Hit indicates that the provider was not called.
func (*LocalCache) Has ¶
func (lc *LocalCache) Has(key interface{}) (has bool)
Has returns if the key is present in the LocalCache.
func (*LocalCache) NotifyStarted ¶
func (lc *LocalCache) NotifyStarted() <-chan struct{}
NotifyStarted returns the underlying started signal.
func (*LocalCache) NotifyStopped ¶
func (lc *LocalCache) NotifyStopped() <-chan struct{}
NotifyStopped returns the underlying stopped signal.
func (*LocalCache) Remove ¶
func (lc *LocalCache) Remove(key interface{}) (value interface{}, hit bool)
Remove removes a specific key.
func (*LocalCache) Reset ¶
func (lc *LocalCache) Reset()
Reset removes all items from the cache, leaving an empty cache.
Reset will call the removed handler for any elements currently in the cache with a removal reason `Removed`. This will be done outside the critical section.
func (*LocalCache) Set ¶
func (lc *LocalCache) Set(key, value interface{}, options ...ValueOption)
Set adds a LocalCache item.
func (*LocalCache) Stats ¶
func (lc *LocalCache) Stats() (stats Stats)
Stats returns the LocalCache stats.
Stats include the number of items held, the age of the items, and the size in bytes represented by each of the items (not including) the fields of the cache itself like the LRU queue.
type LocalCacheOption ¶
type LocalCacheOption func(*LocalCache)
LocalCacheOption is a local cache option.
func OptLRU ¶
func OptLRU(lruImplementation LRU) LocalCacheOption
OptLRU sets the LRU implementation.
func OptSweepInterval ¶
func OptSweepInterval(d time.Duration) LocalCacheOption
OptSweepInterval sets the local cache sweep interval.
type Locker ¶
Locker is a cache type that supports external control of locking for both exclusive and reader/writer locks.
type RemovalReason ¶
type RemovalReason int
RemovalReason is a reason for removal.
const ( Expired RemovalReason = iota Removed RemovalReason = iota )
RemovalReasons
func (RemovalReason) String ¶
func (rr RemovalReason) String() string
String returns a string representation of the removal reason.
type Value ¶
type Value struct { Timestamp time.Time Expires time.Time Key interface{} Value interface{} OnRemove func(interface{}, RemovalReason) }
Value is a cached item.
type ValueOption ¶
type ValueOption func(*Value)
ValueOption is an option for a cache value.
func OptValueExpires ¶
func OptValueExpires(expires time.Time) ValueOption
OptValueExpires sets the ttl for the value.
func OptValueOnRemove ¶
func OptValueOnRemove(handler func(interface{}, RemovalReason)) ValueOption
OptValueOnRemove sets the on remove handler.
func OptValueTTL ¶
func OptValueTTL(d time.Duration) ValueOption
OptValueTTL sets the ttl for the value.
func OptValueTimestamp ¶
func OptValueTimestamp(t time.Time) ValueOption
OptValueTimestamp sets the timestamp for the value.