lru

package
v3.83.5 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2024 License: AGPL-3.0 Imports: 3 Imported by: 0

Documentation

Overview

Package lru provides a generic, size-limited, LRU (Least Recently Used) cache with optional metrics collection and reporting. It wraps the golang-lru/v2 caching library, adding support for custom metrics tracking cache hits, misses, evictions, and other cache operations.

This package supports configuring key aspects of cache behavior, including maximum cache size, and custom metrics collection.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache[T any] struct {
	// contains filtered or unexported fields
}

Cache is a generic LRU-sized cache that stores key-value pairs with a maximum size limit. It wraps the lru.Cache library and adds support for custom metrics collection.

func NewCache

func NewCache[T any](cacheName string, opts ...Option[T]) (*Cache[T], error)

NewCache creates a new Cache with optional configuration parameters. It takes a cache name and a variadic list of options.

func (*Cache[T]) Clear

func (lc *Cache[T]) Clear()

Clear removes all keys from the cache.

func (*Cache[T]) Contents

func (lc *Cache[T]) Contents() string

Contents returns all keys in the cache encoded as a string.

func (*Cache[T]) Count

func (lc *Cache[T]) Count() int

Count returns the number of key-value pairs in the cache.

func (*Cache[T]) Delete

func (lc *Cache[T]) Delete(key string)

Delete removes a key from the cache.

func (*Cache[T]) Exists

func (lc *Cache[T]) Exists(key string) bool

Exists checks if a key exists in the cache.

func (*Cache[T]) Get

func (lc *Cache[T]) Get(key string) (T, bool)

Get retrieves a value from the cache by key.

func (*Cache[T]) Keys

func (lc *Cache[T]) Keys() []string

Keys returns all keys in the cache.

func (*Cache[T]) Set

func (lc *Cache[T]) Set(key string, val T)

Set adds a key-value pair to the cache.

func (*Cache[T]) Values

func (lc *Cache[T]) Values() []T

Values returns all values in the cache.

type Option

type Option[T any] func(*Cache[T])

Option defines a functional option for configuring the Cache.

func WithCapacity

func WithCapacity[T any](capacity int) Option[T]

WithCapacity is a functional option to set the maximum number of items the cache can hold. If the capacity is not set, the default value (128_000) is used.

func WithMetricsCollector

func WithMetricsCollector[T any](collector cache.EvictionMetricsCollector) Option[T]

WithMetricsCollector is a functional option to set a custom metrics collector.

Jump to

Keyboard shortcuts

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