lrucache

package
v0.3.7 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package lrucache provides a very basic LRU cache implementation.

The lru package provides a very basic LRU cache implementation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache interface {
	// Add a new value to the cache and updates the recent-ness.
	// Returns true if an eviction occurred.
	Add(key, value interface{}) bool

	// Return a key's value if found in the cache and updates the recent-ness.
	Get(key interface{}) (value interface{}, ok bool)

	// Check if a key exists without updating the recent-ness.
	Contains(key interface{}) (ok bool)

	// Remove a key from the cache.
	Remove(key interface{}) bool

	// Return a slice with all keys ordered MRU -> LRU.
	Keys() []interface{}

	// Return number of entries in the cache.
	Length() int

	// Remove all entries from the cache.
	Purge()
}

Cache defines the interface for the LRU cache

type LRUCache

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

LRU struct to represent the LRU cache

func NewLRUCache

func NewLRUCache(size int) (*LRUCache, error)

New constructs a new cache instance

func (*LRUCache) Add

func (c *LRUCache) Add(key, value interface{}) (evicted bool)

func (*LRUCache) Contains

func (c *LRUCache) Contains(key interface{}) (ok bool)

func (*LRUCache) Export added in v0.3.6

func (c *LRUCache) Export(w io.Writer) error

Export writes size + entries (MRU -> LRU) in gob format WITHOUT holding locks during encoding.

func (*LRUCache) ExportToFile added in v0.3.6

func (c *LRUCache) ExportToFile(path string) error

ExportToFile writes (non-atomic) to a file path.

func (*LRUCache) ExportToFileAtomic added in v0.3.6

func (c *LRUCache) ExportToFileAtomic(path string) error

ExportToFileAtomic writes to a temp file and atomically renames it.

func (*LRUCache) Get

func (c *LRUCache) Get(key interface{}) (value interface{}, ok bool)

func (*LRUCache) Import added in v0.3.6

func (c *LRUCache) Import(r io.Reader) error

Import replaces the cache contents, preserving LRU order. Assumes Entries are MRU -> LRU (same as Export).

func (*LRUCache) ImportFromFile added in v0.3.6

func (c *LRUCache) ImportFromFile(path string) error

func (*LRUCache) Keys

func (c *LRUCache) Keys() []interface{}

Keys returns keys in MRU -> LRU order (does not change recency).

func (*LRUCache) Length

func (c *LRUCache) Length() int

func (*LRUCache) Purge

func (c *LRUCache) Purge()

func (*LRUCache) Remove

func (c *LRUCache) Remove(key interface{}) bool

func (*LRUCache) Snapshot added in v0.3.6

func (c *LRUCache) Snapshot() (size int, entries []Pair)

Snapshot returns a copy of the cache contents in MRU -> LRU order, plus the configured size. It does NOT change recency.

type Pair added in v0.3.6

type Pair struct {
	Key   interface{}
	Value interface{}
}

Pair is a serializable key/value used for snapshots and export.

Jump to

Keyboard shortcuts

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