eviction

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2023 License: MPL-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var CAWOLFUNodePool = sync.Pool{
	New: func() any {
		return &CAWOLFUNode{}
	},
}

CAWOLFUNodePool is a pool of CAWOLFUNode values.

View Source
var LFUNodePool = sync.Pool{
	New: func() interface{} {
		return &Node{}
	},
}

LFUNodePool is a pool of Node values.

View Source
var LRUCacheItemmPool = sync.Pool{
	New: func() interface{} {
		return &LRUCacheItem{}
	},
}

LRUCacheItemmPool is a pool of LRUCacheItemm values.

Functions

func RegisterEvictionAlgorithm

func RegisterEvictionAlgorithm(name string, createFunc func(capacity int) (IAlgorithm, error))

RegisterEvictionAlgorithm registers a new eviction algorithm with the given name.

func RegisterEvictionAlgorithms added in v0.0.7

func RegisterEvictionAlgorithms(algorithms map[string]func(capacity int) (IAlgorithm, error))

RegisterEvictionAlgorithms registers a set of eviction algorithms.

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

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

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) 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 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 the least recently used item from the cache.

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 IAlgorithm added in v0.0.6

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. If the capacity is negative, it returns an error. The algorithmName parameter is used to select the eviction algorithm from the registry.

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) (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 (*LFUAlgorithm) Set

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

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

func NewLRU

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

NewLRU 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) 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 LRUCacheItem

type LRUCacheItem struct {
	Key   string
	Value any
	// 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.

Jump to

Keyboard shortcuts

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