Documentation
¶
Index ¶
- Variables
- func RegisterEvictionAlgorithm(name string, createFunc func(capacity int) (EvictionAlgorithm, error))
- func RegisterStatsCollector(name string, createFunc func() (StatsCollector, error))
- type ARC
- type CAWOLFU
- type CAWOLFULinkedList
- type CAWOLFUNode
- type CacheItem
- type ClockAlgorithm
- type EvictionAlgorithm
- type FilteringOption
- type HyperCache
- func (cache *HyperCache) Capacity() int
- func (cache *HyperCache) Clear()
- func (cache *HyperCache) Get(key string) (value interface{}, ok bool)
- func (cache *HyperCache) GetMultiple(keys ...string) (result map[string]interface{}, errors map[string]error)
- func (cache *HyperCache) GetOrSet(key string, value interface{}, expiration time.Duration) (interface{}, error)
- func (cache *HyperCache) GetStats() stats.Stats
- func (cache *HyperCache) List(options ...FilteringOption) ([]*CacheItem, error)
- func (cache *HyperCache) Remove(keys ...string)
- func (cache *HyperCache) Set(key string, value interface{}, expiration time.Duration) error
- func (cache *HyperCache) SetCapacity(capacity int)
- func (cache *HyperCache) Size() int
- func (cache *HyperCache) Stop()
- func (cache *HyperCache) TriggerEviction()
- type LFUAlgorithm
- type LRU
- type LRUCacheItem
- type LinkedList
- type Node
- type Option
- type StatsCollector
Constants ¶
This section is empty.
Variables ¶
var ( // ErrInvalidKey is returned when an invalid key is used to access an item in the cache. // An invalid key is a key that is either empty or consists only of whitespace characters. ErrInvalidKey = errors.New("invalid key") // ErrKeyNotFound is returned when a key is not found in the cache. ErrKeyNotFound = errors.New("key not found") // ErrNilValue is returned when a nil value is attempted to be set in the cache. ErrNilValue = errors.New("nil value") // ErrKeyExpired is returned when a key is found in the cache but has expired. ErrKeyExpired = errors.New("key expired") // ErrInvalidExpiration is returned when an invalid expiration is passed to a cache item. ErrInvalidExpiration = errors.New("expiration cannot be negative") // ErrInvalidCapacity is returned when an invalid capacity is passed to the cache. ErrInvalidCapacity = errors.New("capacity cannot be negative") // ErrAlgorithmNotFound is returned when an algorithm is not found. ErrAlgorithmNotFound = errors.New("algorithm not found") // ErrStatsCollectorNotFound is returned when an algorithm is not found. ErrStatsCollectorNotFound = errors.New("stats collector not found") // ErrParamCannotBeEmpty is returned when a parameter cannot be empty. ErrParamCannotBeEmpty = errors.New("param cannot be empty") )
var CAWOLFUNodePool = sync.Pool{ New: func() interface{} { return &CAWOLFUNode{} }, }
CAWOLFUNodePool is a pool of CAWOLFUNode values.
var CacheItemPool = sync.Pool{ New: func() interface{} { return &CacheItem{} }, }
CacheItemPool is a pool of CacheItem values.
var LFUNodePool = sync.Pool{ New: func() interface{} { return &Node{} }, }
LFUNodePool is a pool of Node values.
var LRUCacheItemmPool = sync.Pool{ New: func() interface{} { return &LRUCacheItem{} }, }
LRUCacheItemmPool is a pool of LRUCacheItemm values.
var StatsCollectorRegistry = make(map[string]func() (StatsCollector, error))
StatsCollectorRegistry holds the a registry of stats collectors.
Functions ¶
func RegisterEvictionAlgorithm ¶
func RegisterEvictionAlgorithm(name string, createFunc func(capacity int) (EvictionAlgorithm, error))
RegisterEvictionAlgorithm registers a new eviction algorithm with the given name.
func RegisterStatsCollector ¶
func RegisterStatsCollector(name string, createFunc func() (StatsCollector, error))
RegisterStatsCollector registers a new stats collector with the given name.
Types ¶
type ARC ¶
type ARC struct {
// contains filtered or unexported fields
}
ARC is an in-memory cache that uses the Adaptive Replacement Cache (ARC) algorithm to manage its items.
func NewARC ¶
NewARC creates a new in-memory cache with the given capacity and the Adaptive Replacement Cache (ARC) algorithm. If the capacity is negative, it returns an error.
func (*ARC) Evict ¶
Evict removes an item from the cache and returns the key of the evicted item. If no item can be evicted, it returns an error.
type CAWOLFU ¶
type CAWOLFU struct {
// contains filtered or unexported fields
}
CAWOLFU is an eviction algorithm that uses the Cache-Aware Write-Optimized LFU (CAWOLFU) policy to select items for eviction.
func NewCAWOLFU ¶
NewCAWOLFU returns a new CAWOLFU with the given capacity.
type CAWOLFULinkedList ¶
type CAWOLFULinkedList struct {
// contains filtered or unexported fields
}
CAWOLFULinkedList is a struct that represents a linked list. It has a head and tail field.
type CAWOLFUNode ¶
type CAWOLFUNode struct {
// contains filtered or unexported fields
}
CAWOLFUNode is a struct that represents a node in the linked list. It has a key, value, and access count field.
type CacheItem ¶
type CacheItem struct { // Key string // key of the item Value interface{} // value of the item Expiration time.Duration // expiration duration of the item // Expiration int64 // monotonic clock value in nanoseconds LastAccess time.Time // last access time of the item AccessCount uint // number of times the item has been accessed }
CacheItem is a struct that represents an item in the cache. It has a key, value, expiration duration, and a last access time field.
func (*CacheItem) FieldByName ¶
FieldByName returns the value of the field of the CacheItem struct with the given name. If the field does not exist, an empty reflect.Value is returned.
type ClockAlgorithm ¶
type ClockAlgorithm struct {
// contains filtered or unexported fields
}
ClockAlgorithm is an in-memory cache with the Clock algorithm.
func NewClockAlgorithm ¶
func NewClockAlgorithm(capacity int) (*ClockAlgorithm, error)
NewClockAlgorithm creates a new in-memory cache with the given capacity and the Clock algorithm.
func (*ClockAlgorithm) Delete ¶
func (c *ClockAlgorithm) Delete(key string)
Delete deletes the item with the given key from the cache.
func (*ClockAlgorithm) Evict ¶
func (c *ClockAlgorithm) Evict() (string, bool)
Evict evicts the least recently used item from the cache.
func (*ClockAlgorithm) Get ¶
func (c *ClockAlgorithm) Get(key string) (interface{}, bool)
Get retrieves the item with the given key from the cache.
func (*ClockAlgorithm) Set ¶
func (c *ClockAlgorithm) Set(key string, value interface{})
Set sets the item with the given key and value in the cache.
type EvictionAlgorithm ¶
type EvictionAlgorithm interface { // Evict returns the next item to be evicted from the cache. Evict() (string, bool) // Set adds a new item to the cache with the given key. Set(key string, value interface{}) // Get retrieves the item with the given key from the cache. Get(key string) (interface{}, bool) // Delete removes the item with the given key from the cache. Delete(key string) }
EvictionAlgorithm is the interface that must be implemented by eviction algorithms.
func NewEvictionAlgorithm ¶
func NewEvictionAlgorithm(algorithmName string, capacity int) (EvictionAlgorithm, error)
NewEvictionAlgorithm creates a new eviction algorithm with the given capacity. If the capacity is negative, it returns an error. The algorithmName parameter is used to select the eviction algorithm from the registry.
type FilteringOption ¶
type FilteringOption func(*HyperCache)
FilteringOption is a function type that can be used to filter out the items held in the `HyperCache`.
func WithFilter ¶
func WithFilter(fn func(item *CacheItem) bool) FilteringOption
WithFilter is an option that sets the filter function to use. The filter function is a predicate that takes a `CacheItem` as an argument and returns a boolean indicating whether the item should be included in the cache.
func WithSortAscending ¶
func WithSortAscending() FilteringOption
WithSortAscending is an option that sets the sort order to ascending. When sorting the items in the cache, they will be sorted in ascending order based on the field specified with the `WithSortBy` option.
func WithSortBy ¶
func WithSortBy(field types.SortingField) FilteringOption
WithSortBy is an option that sets the field to sort the items by. The field can be any of the fields in the `CacheItem` struct.
func WithSortDescending ¶
func WithSortDescending() FilteringOption
WithSortDescending is an option that sets the sort order to descending. When sorting the items in the cache, they will be sorted in descending order based on the field specified with the `WithSortBy` option.
type HyperCache ¶
type HyperCache struct {
// contains filtered or unexported fields
}
HyperCache is an in-memory cache that stores items with a key and expiration duration. It has a custom ConcurrentMap to store the items in the cache, and a capacity field that limits the number of items that can be stored in the cache. The stop channel is used to signal the expiration and eviction loops to stop. The evictCh channel is used to signal the eviction loop to start.
func NewHyperCache ¶
func NewHyperCache(capacity int, options ...Option) (cache *HyperCache, err error)
NewHyperCache creates a new in-memory cache with the given capacity. If the capacity is negative, it returns an error. The function initializes the items map, and starts the expiration and eviction loops in separate goroutines.
func (*HyperCache) Capacity ¶
func (cache *HyperCache) Capacity() int
Capacity returns the capacity of the cache.
func (*HyperCache) Get ¶
func (cache *HyperCache) Get(key string) (value interface{}, ok bool)
Get retrieves the item with the given key from the cache. If the item is not found, it returns nil.
func (*HyperCache) GetMultiple ¶
func (cache *HyperCache) GetMultiple(keys ...string) (result map[string]interface{}, errors map[string]error)
GetMultiple retrieves the items with the given keys from the cache. If an item is not found, it is not included in the returned map.
func (*HyperCache) GetOrSet ¶
func (cache *HyperCache) GetOrSet(key string, value interface{}, expiration time.Duration) (interface{}, error)
GetOrSet retrieves the item with the given key from the cache. If the item is not found, it adds the item to the cache with the given value and expiration duration. If the capacity of the cache is reached, the cache will evict the least recently used item before adding the new item.
func (*HyperCache) GetStats ¶ added in v0.0.4
func (cache *HyperCache) GetStats() stats.Stats
GetStats returns the stats collected by the cache. It returns a map where the keys are the stat names and the values are the stat values.
func (*HyperCache) List ¶
func (cache *HyperCache) List(options ...FilteringOption) ([]*CacheItem, error)
List lists the items in the cache that meet the specified criteria.
func (*HyperCache) Remove ¶
func (cache *HyperCache) Remove(keys ...string)
Remove removes items with the given key from the cache. If an item is not found, it does nothing.
func (*HyperCache) Set ¶
func (cache *HyperCache) Set(key string, value interface{}, expiration time.Duration) error
Set adds an item to the cache with the given key and value. If an item with the same key already exists, it updates the value of the existing item. If the expiration duration is greater than zero, the item will expire after the specified duration. If the capacity of the cache is reached, the cache will evict the least recently used item before adding the new item.
func (*HyperCache) SetCapacity ¶
func (cache *HyperCache) SetCapacity(capacity int)
SetCapacity sets the capacity of the cache. If the new capacity is smaller than the current number of items in the cache, it evicts the excess items from the cache.
func (*HyperCache) Size ¶
func (cache *HyperCache) Size() int
Size returns the number of items in the cache.
func (*HyperCache) Stop ¶
func (cache *HyperCache) Stop()
Stop function stops the expiration and eviction loops and closes the stop channel.
func (*HyperCache) TriggerEviction ¶
func (cache *HyperCache) TriggerEviction()
TriggerEviction sends a signal to the eviction loop to start.
type LFUAlgorithm ¶
type LFUAlgorithm struct {
// contains filtered or unexported fields
}
LFUAlgorithm is an eviction algorithm that uses the Least Frequently Used (LFU) policy to select items for eviction.
func NewLFUAlgorithm ¶
func NewLFUAlgorithm(capacity int) (*LFUAlgorithm, error)
NewLFUAlgorithm returns a new LFUAlgorithm with the given capacity.
func (*LFUAlgorithm) Delete ¶
func (l *LFUAlgorithm) Delete(key string)
Delete removes the given key from the cache.
func (*LFUAlgorithm) Evict ¶
func (l *LFUAlgorithm) Evict() (string, bool)
Evict returns the next item to be evicted from the cache.
func (*LFUAlgorithm) Get ¶
func (l *LFUAlgorithm) Get(key string) (interface{}, bool)
Get returns the value for the given key from the cache. If the key is not in the cache, it returns false.
func (*LFUAlgorithm) Set ¶
func (l *LFUAlgorithm) Set(key string, value interface{})
Set adds a new item to the cache with the given key.
type LRU ¶
type LRU struct {
// contains filtered or unexported fields
}
LRU represents a LRU cache
type LRUCacheItem ¶
type LRUCacheItem struct { Key string Value interface{} // contains filtered or unexported fields }
LRUCacheItem represents an item in the LRU cache
type LinkedList ¶
type LinkedList struct {
// contains filtered or unexported fields
}
LinkedList is a struct that represents a linked list. It has a head and tail field.
type Node ¶
type Node struct {
// contains filtered or unexported fields
}
Node is a struct that represents a node in the linked list. It has a key, value, and access count field.
type Option ¶
type Option func(*HyperCache)
Option is a function type that can be used to configure the `HyperCache` struct.
func EvictionAlgorithmName ¶
EvictionAlgorithmName is an option that sets the eviction algorithm name field of the `HyperCache` struct. The eviction algorithm name determines which eviction algorithm will be used to evict items from the cache. The eviction algorithm name must be one of the following: - "LRU" (Least Recently Used) - Implemented in the `lru.go` file - "LFU" (Least Frequently Used) - Implemented in the `lfu.go` file - "CAWOLFU" (Cache-Aware Write-Optimized LFU) - Implemented in the `cawolfu.go` file - "FIFO" (First In First Out) - "RANDOM" (Random) - "CLOCK" (Clock) - Implemented in the `clock.go` file - "ARC" (Adaptive Replacement Cache) - Implemented in the `arc.go` file - "TTL" (Time To Live) - "LFUDA" (Least Frequently Used with Dynamic Aging) - "SLRU" (Segmented Least Recently Used)
func WithEvictionInterval ¶
WithEvictionInterval is an option that sets the eviction interval field of the `HyperCache` struct. The eviction interval determines how often the cache will run the eviction process to remove the least recently used items.
func WithExpirationInterval ¶
WithExpirationInterval is an option that sets the expiration interval field of the `HyperCache` struct. The expiration interval determines how often the cache will check for and remove expired items.
func WithMaxEvictionCount ¶
WithMaxEvictionCount is an option that sets the max eviction count field of the `HyperCache` struct. The max eviction count determines the maximum number of items that can be removed during a single eviction run.
func WithStatsCollector ¶
WithStatsCollector is an option that sets the stats collector field of the `HyperCache` struct. The stats collector is used to collect statistics about the cache.
type StatsCollector ¶
type StatsCollector interface { // Incr increments the count of a statistic by the given value. Incr(stat types.Stat, value int64) // Decr decrements the count of a statistic by the given value. Decr(stat types.Stat, value int64) // Timing records the time it took for an event to occur. Timing(stat types.Stat, value int64) // Gauge records the current value of a statistic. Gauge(stat types.Stat, value int64) // Histogram records the statistical distribution of a set of values. Histogram(stat types.Stat, value int64) // GetStats returns the collected statistics. GetStats() stats.Stats }
StatsCollector is an interface that defines the methods that a stats collector should implement.
func NewStatsCollector ¶
func NewStatsCollector(statsCollectorName string) (StatsCollector, error)
NewStatsCollector creates a new stats collector. The statsCollectorName parameter is used to select the stats collector from the registry.