policy

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 18, 2025 License: MIT Imports: 5 Imported by: 1

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]) Capacity

func (p *FIFO[K, V]) Capacity() int

Capacity returns the maximum number of items the policy can hold

func (*FIFO[K, V]) Evict

func (p *FIFO[K, V]) Evict() (K, bool)

Evict returns the next key to be evicted from the cache

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

func (*FIFO[K, V]) OnSet

func (p *FIFO[K, V]) OnSet(key K, value V, ttl time.Duration)

OnSet is called when an item is added to the cache

func (*FIFO[K, V]) Size

func (p *FIFO[K, V]) Size() int

Size returns the number of items in the policy

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]) Capacity

func (p *LFU[K, V]) Capacity() int

Capacity returns the maximum number of items the policy can hold

func (*LFU[K, V]) Evict

func (p *LFU[K, V]) Evict() (K, bool)

Evict returns the next key to be evicted from the cache

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

func (*LFU[K, V]) OnSet

func (p *LFU[K, V]) OnSet(key K, value V, ttl time.Duration)

OnSet is called when an item is added to the cache

func (*LFU[K, V]) Size

func (p *LFU[K, V]) Size() int

Size returns the number of items in the policy

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]) Capacity

func (p *LRU[K, V]) Capacity() int

Capacity returns the maximum number of items the policy can hold

func (*LRU[K, V]) Evict

func (p *LRU[K, V]) Evict() (K, bool)

Evict returns the next key to be evicted from the cache

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

func (*LRU[K, V]) OnSet

func (p *LRU[K, V]) OnSet(key K, value V, ttl time.Duration)

OnSet is called when an item is added to the cache

func (*LRU[K, V]) Size

func (p *LRU[K, V]) Size() int

Size returns the number of items in the policy

type Option

type Option func(*Options)

Option is a function that configures policy options

func WithDefaultTTL

func WithDefaultTTL(ttl time.Duration) Option

WithDefaultTTL sets the default TTL for entries

func WithMaxSize

func WithMaxSize(size int) Option

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

func NewLFU

func NewLFU[K comparable, V any](opts ...Option) Policy[K, V]

NewLFU creates a new LFU policy

func NewLRU

func NewLRU[K comparable, V any](opts ...Option) Policy[K, V]

NewLRU creates a new LRU policy

Jump to

Keyboard shortcuts

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