lru

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2025 License: BSD-3-Clause Imports: 1 Imported by: 0

Documentation

Overview

Copyright 2025 Kristopher Rahim Afful-Brown. All rights reserved.

Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LRU

type LRU[K comparable, V any] struct {
	// MaxEntries is the maximum number of cache entries before
	// an item is evicted. Zero means no limit.
	MaxEntries int

	// OnEvicted optionally specifies a callback function to be
	// executed when an entry is purged from the cache.
	OnEvicted func(key K, value V)
	// contains filtered or unexported fields
}

LRU is an LRU cache. It is not safe for concurrent access.

func New

func New[K comparable, V any](maxEntries int) *LRU[K, V]

New creates a new Cache. If maxEntries is zero, the cache has no limit and it's assumed that eviction is done by the caller.

func (*LRU[K, V]) Add

func (c *LRU[K, V]) Add(key K, value V)

Add adds a value to the cache.

func (*LRU[K, V]) Clear

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

Clear purges all stored items from the cache.

func (*LRU[K, V]) Get

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

Get looks up a key's value from the cache.

func (*LRU[K, V]) Len

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

Len returns the number of items in the cache.

func (*LRU[K, V]) Remove

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

Remove removes the provided key from the cache.

func (*LRU[K, V]) RemoveOldest

func (c *LRU[K, V]) RemoveOldest()

RemoveOldest removes the oldest item from the cache.

Jump to

Keyboard shortcuts

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