Documentation ¶
Index ¶
- type FrequencyNode
- type KeyRefNode
- type KeyValueEntry
- type LFUCache
- func (lfu *LFUCache[comparable, any]) AsSlice() *[]KeyValueEntry[comparable, any]
- func (lfu *LFUCache[comparable, any]) CurrentSize() uint
- func (lfu *LFUCache[comparable, any]) Delete(key comparable) error
- func (lfu *LFUCache[comparable, any]) Evict() error
- func (lfu *LFUCache[comparable, any]) FlushLazyCounter() error
- func (lfu *LFUCache[comparable, any]) Get(key comparable) (*any, bool)
- func (lfu *LFUCache[comparable, any]) GetLeastFrequencyItems() *[]KeyValueEntry[comparable, any]
- func (lfu *LFUCache[comparable, any]) GetTopFrequencyItems() *[]KeyValueEntry[comparable, any]
- func (lfu *LFUCache[comparable, any]) IsFull() bool
- func (lfu *LFUCache[comparable, any]) Keys() []comparable
- func (lfu *LFUCache[comparable, any]) MaxSize() uint
- func (lfu *LFUCache[comparable, any]) Put(key comparable, value any, replace bool) error
- func (lfu *LFUCache[comparable, any]) SetMaxSize(size uint)
- type LFULazyCounter
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type FrequencyNode ¶
type FrequencyNode struct {
// contains filtered or unexported fields
}
`FrequencyNode` represents a node in the frequency linked list
type KeyRefNode ¶
type KeyRefNode[K comparable, V any] struct { // contains filtered or unexported fields }
`KeyRefNode“ represents the value held on the LRU cache frequency list node
type KeyValueEntry ¶
type KeyValueEntry[K comparable, V any] struct { Key *K // pointer to key Value *V // pointer to value Frequency uint // frequency of access }
`KeyValueEntry` represents an item in the slice representation of LFU cache
type LFUCache ¶
type LFUCache[K comparable, V any] struct { // contains filtered or unexported fields }
`LFUCache` implements all the methods and data-structures required for LFU cache
func NewLFUCache ¶
func NewLFUCache[K comparable, V any](maxSize uint) *LFUCache[K, V]
`NewLFUCache` returns the new instance of LFU cache with specified size.
Parameters:
1. maxSize: `uint` representing the max size of LFU cache.
Returns: (*LFUCache) a pointer to LFU cache instance.
func NewLazyLFUCache ¶
func NewLazyLFUCache[K comparable, V any](maxSize uint, lazyCounterSize uint) *LFUCache[K, V]
`NewLFUCache` returns the new instance of LFU cache with specified size and lazy mode enabled.
Parameters:
1. maxSize: `uint` representing the max size of LFU cache.
2. lazyCounterSize: size of lazy counter to use, frequencies will be updated in batch or when some write happens or manually FlushLazyCounter is called. Returns: (*LFUCache) a pointer to LFU cache instance.
func (*LFUCache[comparable, any]) AsSlice ¶
func (lfu *LFUCache[comparable, any]) AsSlice() *[]KeyValueEntry[comparable, any]
`AsSlice` returns the list of all elements in the key lfu cache and their frequencies
Returns: a pointer to the slice of `KeyValueEntry` type, which contains the list of elements (key, value and frequency) in the current state of LFU cache.
func (*LFUCache[comparable, any]) CurrentSize ¶
func (lfu *LFUCache[comparable, any]) CurrentSize() uint
`CurrentSize` returns the number of elements in that cache
Returns: `uint` representing the current size
func (*LFUCache[comparable, any]) Delete ¶
func (lfu *LFUCache[comparable, any]) Delete(key comparable) error
`Delete` removes the specified entry from LFU cache
Parameters:
key: key is of type `KeyType` (or simply `interface{}`) which represents the key to be deleted
Returns: `error` which is nil if `key` is deleted, non-nil if there are some errors while deletion
func (*LFUCache[comparable, any]) Evict ¶
func (lfu *LFUCache[comparable, any]) Evict() error
`Evict` can be called to manually perform eviction
Returns: `error` if there are any errors during eviction
func (*LFUCache[comparable, any]) FlushLazyCounter ¶
func (lfu *LFUCache[comparable, any]) FlushLazyCounter() error
FlushLazyCounter updates the state LFU cache with pending frequency updates in lazy counter
Returns: error if lazy update fails
func (*LFUCache[comparable, any]) Get ¶
func (lfu *LFUCache[comparable, any]) Get(key comparable) (*any, bool)
`Get` can be called to obtain the value for given key
Parameters:
key: key: Key is of `KeyType` (or simply an `interface{}`) which represents the key, note that the key must be hashable type
Returns: `(*ValueType, bool)` - returns a pointer to the value in LFU cache if `key` exists, else it will be `nil` with `error` non-nil.
func (*LFUCache[comparable, any]) GetLeastFrequencyItems ¶
func (lfu *LFUCache[comparable, any]) GetLeastFrequencyItems() *[]KeyValueEntry[comparable, any]
`GetLeastFrequencyItems` returns the list of all elements in the key lfu cache and their frequencies
Returns: a pointer to the slice of `KeyValueEntry` type, which contains the list of elements (key, value and frequency) having least frequency value in the current state of the LFU cache.
func (*LFUCache[comparable, any]) GetTopFrequencyItems ¶
func (lfu *LFUCache[comparable, any]) GetTopFrequencyItems() *[]KeyValueEntry[comparable, any]
`GetTopFrequencyItems` returns the list of all elements in the key lfu cache and their frequencies
Returns: a pointer to the slice of `KeyValueEntry` type, which contains the list of elements (key, value and frequency) having highest frequency value in the current state of the LFU cache.
func (*LFUCache[comparable, any]) IsFull ¶
func (lfu *LFUCache[comparable, any]) IsFull() bool
`IsFull` checks if the LFU cache is full
Returns (true/false), `true` if LFU cache is full, `false` if LFU cache is not full
func (*LFUCache[comparable, any]) Keys ¶
func (lfu *LFUCache[comparable, any]) Keys() []comparable
func (*LFUCache[comparable, any]) MaxSize ¶
func (lfu *LFUCache[comparable, any]) MaxSize() uint
`MaxSize` returns the maximum size of the cache at that point in time
func (*LFUCache[comparable, any]) Put ¶
func (lfu *LFUCache[comparable, any]) Put(key comparable, value any, replace bool) error
`Put` method inserts a `<KeyType, ValueType>` to the LFU cache and updates internal data structures to keep track of access frequencies, if the cache is full, it evicts the least frequently used value from the cache.
Parameters:
1. key: Key is of `KeyType` (or simply an `interface{}`) which represents the key, note that the key must be hashable type.
2. value: Value is of `ValueType` (or simply an `interface{}`) which represents the value
3. replace: replace is a `bool`, if set to `true`, the `value` of the given `key` will be overwritten if exists, if set to `false`, an `error` is thrown if `key` already exists.
Returns: `error` if there are any errors during insertions
func (*LFUCache[comparable, any]) SetMaxSize ¶
func (lfu *LFUCache[comparable, any]) SetMaxSize(size uint)
`SetMaxSize` updates the max size of the LFU cache
Parameters ¶
1. size: `uint` value which specifies the new size of the LFU cache
type LFULazyCounter ¶
type LFULazyCounter[K comparable] struct { // contains filtered or unexported fields }