Documentation
¶
Overview ¶
Package cache implements a LRU cache.
The implementation borrows heavily from SmallLRUCache (originally by Nathan Schrenk). The object maintains a doubly-linked list of elements. When an element is accessed, it is promoted to the head of the list. When space is needed, the element at the tail of the list (the least recently used element) is evicted.
Index ¶
- Variables
- type GetOptFn
- type Item
- type LRUCache
- func (lru *LRUCache) Capacity() int64
- func (lru *LRUCache) Clear()
- func (lru *LRUCache) Delete(key any) bool
- func (lru *LRUCache) Evictions() int64
- func (lru *LRUCache) Exist(key any) bool
- func (lru *LRUCache) Get(key any) (v Value, ok bool)
- func (lru *LRUCache) Init(capacity int64)
- func (lru *LRUCache) Items() []Item
- func (lru *LRUCache) Keys() []any
- func (lru *LRUCache) Length() int64
- func (lru *LRUCache) Peek(key any) (v Value, ok bool)
- func (lru *LRUCache) Set(key any, value Value)
- func (lru *LRUCache) SetAndGetRemoved(key any, value Value) (removedValueList []Value)
- func (lru *LRUCache) SetCapacity(capacity int64)
- func (lru *LRUCache) SetIfAbsent(key any, value Value)
- func (lru *LRUCache) Size() int64
- func (lru *LRUCache) Stats() (length, size, capacity, evictions int64)
- func (lru *LRUCache) StatsJSON() string
- type LRUFacade
- type Map
- type MapFacade
- type SetOptFn
- type TTLCache
- type Value
- type WideLRUCache
- type WideMap
Constants ¶
This section is empty.
Variables ¶
var ( // ErrTTLKeyExists When key exists will return this error. ErrTTLKeyExists = status.Error(codes.AlreadyExists, "ttl.key.exists") // ErrTTLKeyNotFound When key not found will return this error. ErrTTLKeyNotFound = status.Error(codes.NotFound, "ttl.key.not.found") )
Functions ¶
This section is empty.
Types ¶
type GetOptFn ¶ added in v0.8.1
type GetOptFn func(*getOption)
func WithRemoveAfterGet ¶ added in v0.8.1
func WithRemoveAfterGet() GetOptFn
func WithUpdateTTL ¶ added in v0.8.3
type LRUCache ¶
type LRUCache struct {
// contains filtered or unexported fields
}
LRUCache is a typical LRU cache implementation. If the cache reaches the capacity, the least recently used item is deleted from the cache. Note the capacity is not the number of items, but the total sum of the Size() of each item.
func NewLRUCache ¶
NewLRUCache creates a new empty cache with the given capacity.
func (*LRUCache) Get ¶
Get returns a value from the cache, and marks the entry as most recently used.
func (*LRUCache) Items ¶
Items returns all the values for the cache, ordered from most recently used to last recently used.
func (*LRUCache) Keys ¶
Keys returns all the keys for the cache, ordered from most recently used to last recently used.
func (*LRUCache) SetAndGetRemoved ¶
SetAndGetRemoved sets a value in the cache and returns the removed value list
func (*LRUCache) SetCapacity ¶
SetCapacity will set the capacity of the cache. If the capacity is smaller, and the current cache size exceed that capacity, the cache will be shrank.
func (*LRUCache) SetIfAbsent ¶
SetIfAbsent will set the value in the cache if not present. If the value exists in the cache, we don't set it.
type LRUFacade ¶ added in v0.2.7
type LRUFacade interface {
// Get returns a value from the cache, and marks the entry as most recently used.
Get(key any) (v Value, ok bool)
// Peek returns a value from the cache without changing the LRU order.
Peek(key any) (v Value, ok bool)
// Exist : return true if key in map
Exist(key any) bool
// Set sets a value in the cache.
Set(key any, value Value)
// Delete removes an entry from the cache, and returns if the entry existed.
Delete(key any) bool
}
LRUFacade an interface to define a LRU cache
func NeWideLRUCache ¶ added in v0.2.7
NeWideLRUCache new wide lru cache
func NewSingleLRUCache ¶ added in v0.2.7
NewSingleLRUCache create a single lru cache
type MapFacade ¶ added in v0.2.7
type MapFacade interface {
//Set : set key-value
Set(key any, value any)
//Get : get value
Get(key any) (any, bool)
//Exist : return true if key in map
Exist(key any) bool
//Delete : delete a key
Delete(key any)
}
MapFacade an interface to define a Map
func NewWideMap ¶ added in v0.2.8
NewWideMap new wide Map
func NewWideXHashMap ¶ added in v0.2.7
NewWideXHashMap new wide Map use xxhash as group
type SetOptFn ¶ added in v0.8.1
type SetOptFn func(*setOption)
func WithKeepTTL ¶ added in v0.8.3
func WithKeepTTL() SetOptFn
func WithMustNotExist ¶ added in v0.8.1
func WithMustNotExist() SetOptFn
type TTLCache ¶ added in v0.8.0
type TTLCache interface {
// Set key value into ttl cache.
Set(ctx context.Context, key string, value []byte, fns ...SetOptFn) error
// Get value by key from ttl cache.
Get(ctx context.Context, key string, fns ...GetOptFn) ([]byte, error)
// Remove value by key.
Remove(ctx context.Context, key string) error
// Clear cache.
Clear(ctx context.Context)
}
func NewTTLMemCache ¶ added in v0.8.2
NewTTLMemCache New ttl cache
type Value ¶
type Value interface {
// Size returns how big this value is. If you want to just track
// the cache by number of objects, you may return the size as 1.
Size() int
}
Value is the interface values that go into LRUCache need to satisfy
type WideLRUCache ¶ added in v0.2.7
type WideLRUCache struct {
// contains filtered or unexported fields
}
WideLRUCache use LruCache group array as a wide lru cache
func (*WideLRUCache) Delete ¶ added in v0.2.7
func (w *WideLRUCache) Delete(key any) bool
Delete removes an entry from the cache, and returns if the entry existed.
func (*WideLRUCache) Exist ¶ added in v1.0.3
func (w *WideLRUCache) Exist(key any) bool
Exist : return true if key in map
func (*WideLRUCache) Get ¶ added in v0.2.7
func (w *WideLRUCache) Get(key any) (v Value, ok bool)
Get returns a value from the cache, and marks the entry as most recently used.
func (*WideLRUCache) Peek ¶ added in v0.2.7
func (w *WideLRUCache) Peek(key any) (v Value, ok bool)
Peek returns a value from the cache without changing the LRU order.
func (*WideLRUCache) Set ¶ added in v0.2.7
func (w *WideLRUCache) Set(key any, value Value)
Set sets a value in the cache.