lru

package
v0.17.1 Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2024 License: BSD-3-Clause Imports: 3 Imported by: 0

Documentation

Overview

The lru package implements a fixed-size in-memory LRU cache.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

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

A Cache is a fixed-size in-memory LRU cache, storing values of type V keyed by keys of type K.

func New

func New[K comparable, V any](capacity int) *Cache[K, V]

New creates a new Cache with the given capacity, which must be positive.

The cache capacity uses arbitrary units, which are specified during the Set operation.

func (*Cache[K, V]) Get

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

Get retrieves the value for the specified key. If the key is found, its access time is updated.

The second result reports whether the key was found.

func (*Cache[K, V]) Set

func (c *Cache[K, V]) Set(key K, value V, size int)

Set stores a value for the specified key, using its given size to update the current cache size, evicting old entries as necessary to fit in the cache capacity.

Size must be a non-negative value. If size is larger than the cache capacity, the value is not stored and the cache is not modified.

Jump to

Keyboard shortcuts

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