eviction

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 15, 2025 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Eviction

type Eviction interface {
	// Get retrieves a value from the eviction cache given a key.
	Get(key string) (any, bool)
	// Put adds a key-value pair to the eviction cache.
	Put(key string, value any)
	// Delete removes a key from the eviction cache.
	Delete(key string)
	// Clear clears the eviction cache
	Clear()
}

Eviction is the interface that defines the methods for an eviction policy.

func New

func New(opt Options) Eviction

New creates a new Eviction instance based on the provided options.

type LFUResource

type LFUResource interface {
	// Key returns key for the eviction entry.
	Key() string
	// IncrementFrequency increments the key access frequency by 1.
	IncrementFrequency()
	// Frequency returns the frequency for the entry.
	Frequency() int
	// Value return the value for the entry.
	Value() any
	// Set sets the value for the entry.
	Set(any)
}

type Options

type Options struct {
	// Capacity is the maximum number of items that can be stored in the eviction cache.
	// After this capacity is reached, the cache will start evicting items based on the eviction policy.
	// Default value is 100.
	Capacity int

	// Policy is the eviction policy to be used.
	Policy Policy

	// DeleteFinalizer is the finalizer function that is called when an item is deleted from the cache.
	DeleteFinalizer func(key string, value any)

	// EvictFinalizer is the finalizer function that is called when an item is evicted from the cache.
	EvictFinalizer func(key string, value any)
}

Options holds the configuration for the eviction cache.

type Policy

type Policy string
const (
	PolicyLRU  Policy = "lru"  // Least Recently Used
	PolicyFIFO Policy = "fifo" // First In First Out
	PolicyLFU  Policy = "lfu"  // Least Frequently Used
	PolicyARC  Policy = "arc"  // Adaptive Replacement Cache
)

type PriorityQueue

type PriorityQueue []*pqItem

PriorityQueue is a priority queue of pqItems

func (PriorityQueue) Len

func (pq PriorityQueue) Len() int

Len returns the length of the priority queue

func (PriorityQueue) Less

func (pq PriorityQueue) Less(i, j int) bool

Less returns true if the item at index i expires before the item at index j

func (*PriorityQueue) Pop

func (pq *PriorityQueue) Pop() any

Pop pops an item from the priority queue

func (*PriorityQueue) Push

func (pq *PriorityQueue) Push(x any)

Push pushes an item onto the priority queue

func (*PriorityQueue) Remove

func (pq *PriorityQueue) Remove(key string)

Remove removes items from the priority queue given the keys

func (PriorityQueue) Swap

func (pq PriorityQueue) Swap(i, j int)

Swap swaps the items at index i and j

type TTL

type TTL interface {
	Len() int
	Top() TTLItem
	Put(key string, value any)
	Delete(key string)
	Pop() string
	Clear()
}

func NewTTL

func NewTTL() TTL

type TTLItem

type TTLItem interface {
	Key() string
	Value() any
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL