Documentation
¶
Overview ¶
Package policy provides different cache eviction policies like FIFO, LRU, and LFU.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Entry ¶
type Entry[V any] struct { Value V `json:"value"` Expires time.Time `json:"expires"` CreatedAt time.Time `json:"createdAt"` LastAccess time.Time `json:"lastAccess"` AccessCount int64 `json:"accessCount"` }
Entry represents a cache entry with metadata for policy decisions
type FIFO ¶
type FIFO[K comparable, V any] struct { // contains filtered or unexported fields }
FIFO implements the Policy interface using First In First Out strategy
func (*FIFO[K, V]) OnClear ¶
func (p *FIFO[K, V]) OnClear()
OnClear is called when the cache is cleared
func (*FIFO[K, V]) OnDelete ¶
func (p *FIFO[K, V]) OnDelete(key K)
OnDelete is called when an item is removed from the cache
func (*FIFO[K, V]) OnGet ¶
func (p *FIFO[K, V]) OnGet(key K, value V)
OnGet is called when an item is retrieved from the cache
type LFU ¶
type LFU[K comparable, V any] struct { // contains filtered or unexported fields }
LFU implements the Policy interface using Least Frequently Used strategy
func (*LFU[K, V]) OnClear ¶
func (p *LFU[K, V]) OnClear()
OnClear is called when the cache is cleared
func (*LFU[K, V]) OnDelete ¶
func (p *LFU[K, V]) OnDelete(key K)
OnDelete is called when an item is removed from the cache
func (*LFU[K, V]) OnGet ¶
func (p *LFU[K, V]) OnGet(key K, value V)
OnGet is called when an item is retrieved from the cache
type LRU ¶
type LRU[K comparable, V any] struct { // contains filtered or unexported fields }
LRU implements the Policy interface using Least Recently Used strategy
func (*LRU[K, V]) OnClear ¶
func (p *LRU[K, V]) OnClear()
OnClear is called when the cache is cleared
func (*LRU[K, V]) OnDelete ¶
func (p *LRU[K, V]) OnDelete(key K)
OnDelete is called when an item is removed from the cache
func (*LRU[K, V]) OnGet ¶
func (p *LRU[K, V]) OnGet(key K, value V)
OnGet is called when an item is retrieved from the cache
type Option ¶
type Option func(*Options)
Option is a function that configures policy options
func WithDefaultTTL ¶
WithDefaultTTL sets the default TTL for entries
func WithMaxSize ¶
WithMaxSize sets the maximum size of the policy
type Options ¶
type Options struct {
// MaxSize is the maximum number of items the policy can hold
MaxSize int
// DefaultTTL is the default time-to-live for entries
DefaultTTL time.Duration
}
Options represents policy configuration options
type Policy ¶
type Policy[K comparable, V any] interface { // OnGet is called when an item is retrieved from the cache OnGet(key K, value V) // OnSet is called when an item is added to the cache OnSet(key K, value V, ttl time.Duration) // OnDelete is called when an item is removed from the cache OnDelete(key K) // OnClear is called when the cache is cleared OnClear() // Evict returns the next key to be evicted from the cache Evict() (K, bool) // Size returns the number of items in the policy Size() int // Capacity returns the maximum number of items the policy can hold Capacity() int }
Policy defines the interface for cache eviction policies
func NewFIFO ¶
func NewFIFO[K comparable, V any](opts ...Option) Policy[K, V]
NewFIFO creates a new FIFO policy