eviction

package
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Aug 20, 2025 License: MPL-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package eviction ARC is an in-memory cache that uses the Adaptive Replacement Cache (ARC) algorithm to manage its items. It has a map of items 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 ARC algorithm uses two lists, t1 and t2, to store the items in the cache. The p field represents the "promotion threshold", which determines how many items should be stored in t1. The c field represents the current number of items in the cache.

Package eviction CAWOLFU is an eviction algorithm that uses the Cache-Aware Write-Optimized LFU (CAWOLFU) policy to select items for eviction.

Package eviction - The clock eviction algorithm is a page replacement algorithm that uses a clock-like data structure to keep track of which pages in a computer's memory have been used recently and which have not. It works by maintaining a circular buffer of pages, with a "hand" that points to the next page to be replaced. When a page needs to be evicted from memory, the hand is advanced to the next page in the buffer, and that page is evicted if it has not been used recently. If the page has been used recently, the hand is advanced to the next page, and the process repeats until a page is found that can be evicted. The clock eviction algorithm is often used in virtual memory systems to manage the allocation of physical memory.

Package eviction - Least Frequently Used (LFU) eviction algorithm implementation

Package eviction - The Least Recently Used (LRU) eviction algorithm is a page replacement algorithm that discards the least recently used pages first. It works by maintaining a queue of pages in memory, with the most recently used page at the front of the queue and the least recently used page at the back. When a new page is added to memory and the memory is full, the page at the back of the queue (the least recently used page) is removed to make space for the new page. To implement the LRU eviction algorithm, a data structure called a doubly linked list is often used. Each page in the list is represented by a node, which contains a pointer to the previous node and a pointer to the next node. When a page is accessed, it is moved to the front of the list by updating the pointers of the surrounding nodes. This way, the page at the back of the list is always the least recently used page, and can be easily removed when necessary. The LRU eviction algorithm is widely used because it performs well in practice, with a low average page fault rate. However, it can be somewhat expensive to implement, as it requires updating the data structure every time a page is accessed.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

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 NewARCAlgorithm

func NewARCAlgorithm(capacity int) (*ARC, error)

NewARCAlgorithm 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) Delete

func (arc *ARC) Delete(key string)

Delete removes the item with the given key from the cache.

func (*ARC) Evict

func (arc *ARC) Evict() (string, bool)

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.

func (*ARC) Get

func (arc *ARC) Get(key string) (any, bool)

Get retrieves the item with the given key from the cache. If the key is not found in the cache, it returns nil.

func (*ARC) Set

func (arc *ARC) Set(key string, value any)

Set adds a new item to the cache with the given key.

type AlgorithmRegistry

type AlgorithmRegistry struct {
	// contains filtered or unexported fields
}

AlgorithmRegistry manages eviction algorithm constructors.

func NewAlgorithmRegistry

func NewAlgorithmRegistry() *AlgorithmRegistry

NewAlgorithmRegistry creates a new algorithm registry.

func NewEmptyAlgorithmRegistry

func NewEmptyAlgorithmRegistry() *AlgorithmRegistry

NewEmptyAlgorithmRegistry creates a new algorithm registry without default algorithms. This is useful for testing or when you want to register only specific algorithms.

func (*AlgorithmRegistry) NewAlgorithm

func (r *AlgorithmRegistry) NewAlgorithm(algorithmName string, capacity int) (IAlgorithm, error)

NewAlgorithm creates a new eviction algorithm with the given capacity.

func (*AlgorithmRegistry) Register

func (r *AlgorithmRegistry) Register(name string, createFunc func(capacity int) (IAlgorithm, error))

Register registers a new eviction algorithm with the given name.

func (*AlgorithmRegistry) RegisterMultiple

func (r *AlgorithmRegistry) RegisterMultiple(algorithms map[string]func(capacity int) (IAlgorithm, error))

RegisterMultiple registers a set of eviction algorithms.

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

func NewCAWOLFU(capacity int) (*CAWOLFU, error)

NewCAWOLFU returns a new CAWOLFU with the given capacity.

func (*CAWOLFU) Delete

func (c *CAWOLFU) Delete(key string)

Delete removes the given key from the cache.

func (*CAWOLFU) Evict

func (c *CAWOLFU) Evict() (string, bool)

Evict returns the next item to be evicted from the cache.

func (*CAWOLFU) Get

func (c *CAWOLFU) Get(key string) (any, bool)

Get returns the value for the given key from the cache. If the key is not in the cache, it returns false.

func (*CAWOLFU) Set

func (c *CAWOLFU) Set(key string, value any)

Set adds a new item to the cache with the given key.

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 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 an item from the cache based on the Clock algorithm.

func (*ClockAlgorithm) Get

func (c *ClockAlgorithm) Get(key string) (any, bool)

Get retrieves the item with the given key from the cache.

func (*ClockAlgorithm) Set

func (c *ClockAlgorithm) Set(key string, value any)

Set sets the item with the given key and value in the cache.

type FrequencyHeap

type FrequencyHeap []*Node

FrequencyHeap is a heap of Nodes.

func (FrequencyHeap) Len

func (fh FrequencyHeap) Len() int

Len returns the length of the heap.

func (FrequencyHeap) Less

func (fh FrequencyHeap) Less(i, j int) bool

Less returns true if the node at index i has a lower frequency than the node at index j.

func (*FrequencyHeap) Pop

func (fh *FrequencyHeap) Pop() any

Pop removes the last node from the heap.

func (*FrequencyHeap) Push

func (fh *FrequencyHeap) Push(x any)

Push adds a node to the heap.

func (FrequencyHeap) Swap

func (fh FrequencyHeap) Swap(i, j int)

Swap swaps the nodes at index i and j.

type IAlgorithm

type IAlgorithm 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 any)
	// Get retrieves the item with the given key from the cache.
	Get(key string) (any, bool)
	// Delete removes the item with the given key from the cache.
	Delete(key string)
}

IAlgorithm is the interface that must be implemented by eviction algorithms.

func NewEvictionAlgorithm

func NewEvictionAlgorithm(algorithmName string, capacity int) (IAlgorithm, error)

NewEvictionAlgorithm creates a new eviction algorithm with the given capacity. It uses a new registry instance with default algorithms for each call. If the capacity is negative, it returns an error. The algorithmName parameter is used to select the eviction algorithm from the default algorithms.

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 creates a new LFUAlgorithm with the given capacity.

func (*LFUAlgorithm) Delete

func (l *LFUAlgorithm) Delete(key string)

Delete deletes a key-value pair from the cache.

func (*LFUAlgorithm) Evict

func (l *LFUAlgorithm) Evict() (string, bool)

Evict evicts an item from the cache based on the LFU algorithm.

func (*LFUAlgorithm) Get

func (l *LFUAlgorithm) Get(key string) (any, bool)

Get gets a value from the cache.

func (*LFUAlgorithm) Set

func (l *LFUAlgorithm) Set(key string, value any)

Set sets a key-value pair in the cache.

type LRU

type LRU struct {
	sync.RWMutex // The mutex used to protect the cache
	// contains filtered or unexported fields
}

LRU represents a LRU cache.

func NewLRUAlgorithm

func NewLRUAlgorithm(capacity int) (*LRU, error)

NewLRUAlgorithm creates a new LRU cache with the given capacity.

func (*LRU) Delete

func (lru *LRU) Delete(key string)

Delete removes the given key from the cache.

func (*LRU) Evict

func (lru *LRU) Evict() (string, bool)

Evict removes the least recently used item from the cache and returns its key.

func (*LRU) Get

func (lru *LRU) Get(key string) (any, bool)

Get retrieves the value for the given key from the cache. If the key is not.

func (*LRU) Len

func (lru *LRU) Len() int

Len returns the number of items in the cache.

func (*LRU) Set

func (lru *LRU) Set(key string, value any)

Set sets the value for the given key in the cache. If the key is not already in the cache, it is added. If the cache is full, the least recently used item is evicted.

type Node

type Node struct {
	// contains filtered or unexported fields
}

Node is a node in the LFUAlgorithm.

Jump to

Keyboard shortcuts

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