lru

package
v0.0.76 Latest Latest
Warning

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

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

Documentation

Overview

Package lru implements a generic Least Recently Used (LRU) cache with support for "spam" items. The cache maintains a maximum number of items, evicting the least recently used item when the limit is reached. Items can be added, retrieved, and deleted from the cache. Regular items are moved to the front of the cache when accessed or updated, while "spam" items maintain their position. This allows for preferential treatment of non-spam items in terms of retention, while still caching spam items. The cache is safe for concurrent use.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache added in v0.0.31

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

Cache is a generic LRU cache that supports "spam" items.

func NewCache

func NewCache[K comparable, V any](maxItems int) *Cache[K, V]

NewCache creates a new LRU cache with the specified maximum number of items.

func (*Cache[K, V]) Delete added in v0.0.31

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

Delete removes an item from the cache if it exists.

func (*Cache[K, V]) Get added in v0.0.31

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

Get retrieves an item from the cache, moving non-spam items to the front. It returns the value and a boolean indicating whether the key was found.

func (*Cache[K, V]) Set added in v0.0.31

func (c *Cache[K, V]) Set(key K, value V, isSpam bool)

Set adds or updates an item in the cache, evicting the LRU item if necessary. The isSpam parameter determines whether the item should be treated as spam. If the item is spam, it will not be moved to the front of the cache.

Jump to

Keyboard shortcuts

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