cache

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Oct 5, 2025 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Package cache provides a thread-safe LRU cache implementation.

The package offers a single generic cache type that automatically evicts the least recently used items when capacity is reached.

Basic usage:

import "github.com/dmitrymomot/foundation/core/cache"

// Create a cache with capacity of 100 items
c := cache.NewLRUCache[string, int](100)

// Store values
c.Put("key1", 42)
c.Put("key2", 84)

// Retrieve values
if value, found := c.Get("key1"); found {
	fmt.Printf("Found: %d\n", value)
}

// Remove values
if value, removed := c.Remove("key1"); removed {
	fmt.Printf("Removed: %d\n", value)
}

// Check size and clear cache
fmt.Printf("Cache size: %d\n", c.Len())
c.Clear()

The Put method returns the previous value if the key existed:

// Update existing key
oldValue, existed := c.Put("key1", 100)
if existed {
	fmt.Printf("Previous value was: %d\n", oldValue)
}

Eviction Callbacks

Set up callbacks to handle resource cleanup when items are evicted:

c := cache.NewLRUCache[string, *os.File](10)

// Clean up files when evicted
c.SetEvictCallback(func(key string, file *os.File) {
	file.Close()
	fmt.Printf("Closed file: %s\n", key)
})

The eviction callback is also triggered when items are manually removed or when the cache is cleared.

Thread Safety

All cache operations are thread-safe and can be called concurrently from multiple goroutines without additional synchronization.

Performance

LRUCache provides O(1) average-case performance for all operations (Get, Put, Remove) using a combination of hash map and doubly-linked list.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LRUCache

type LRUCache[K comparable, V any] struct {
	// contains filtered or unexported fields
}

LRUCache is a thread-safe LRU cache implementation. When the cache reaches its capacity, the least recently used item is evicted.

func NewLRUCache

func NewLRUCache[K comparable, V any](capacity int) *LRUCache[K, V]

NewLRUCache creates a new LRU cache with the specified capacity. The capacity must be positive, otherwise it panics.

func (*LRUCache[K, V]) Clear

func (c *LRUCache[K, V]) Clear()

Clear removes all items from the cache. If an evict callback is set, it's called for each item.

func (*LRUCache[K, V]) Get

func (c *LRUCache[K, V]) Get(key K) (V, bool)

Get retrieves a value from the cache and marks it as recently used. Returns the value and true if found, zero value and false otherwise.

func (*LRUCache[K, V]) Len

func (c *LRUCache[K, V]) Len() int

Len returns the current number of items in the cache.

func (*LRUCache[K, V]) Put

func (c *LRUCache[K, V]) Put(key K, value V) (V, bool)

Put adds or updates a value in the cache. If the cache is at capacity, the least recently used item is evicted. Returns the previous value if it existed, and a boolean indicating if it existed.

func (*LRUCache[K, V]) Remove

func (c *LRUCache[K, V]) Remove(key K) (V, bool)

Remove removes an item from the cache. Returns the removed value and true if it existed, zero value and false otherwise.

func (*LRUCache[K, V]) SetEvictCallback

func (c *LRUCache[K, V]) SetEvictCallback(fn func(key K, value V))

SetEvictCallback sets a callback function that is called when items are evicted. This is useful for cleanup operations like closing resources.

Jump to

Keyboard shortcuts

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