cache

package
v3.85.0 Latest Latest
Warning

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

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

Documentation

Overview

Package cache provides an interface which can be implemented by different cache types.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BaseMetricsCollector added in v3.82.7

type BaseMetricsCollector interface {
	RecordHit(cacheName string)
	RecordMiss(cacheName string)
	RecordSet(cacheName string)
	RecordDelete(cacheName string)
	RecordClear(cacheName string)
}

BaseMetricsCollector defines the interface for recording cache metrics. Each method corresponds to a specific cache-related operation.

func GetBaseMetricsCollector added in v3.82.7

func GetBaseMetricsCollector() BaseMetricsCollector

GetBaseMetricsCollector returns the singleton baseCollector instance.

type Cache

type Cache[T any] interface {
	// Set stores the given key/value pair.
	Set(key string, val T)
	// Get returns the value for the given key and a boolean indicating if the key was found.
	Get(key string) (T, bool)
	// Exists returns true if the given key exists in the cache.
	Exists(key string) bool
	// Delete the given key from the cache.
	Delete(key string)
	// Clear all key/value pairs from the cache.
	Clear()
	// Count the number of key/value pairs in the cache.
	Count() int
	// Keys returns all keys in the cache.
	Keys() []string
	// Values returns all values in the cache.
	Values() []T
	// Contents returns all keys in the cache encoded as a string.
	Contents() string
}

Cache is used to store key/value pairs.

type EvictionMetricsCollector added in v3.82.7

type EvictionMetricsCollector interface {
	RecordEviction(cacheName string)
}

EvictionMetricsCollector defines the interface for recording cache-specific eviction metrics.

func GetEvictionMetricsCollector added in v3.82.7

func GetEvictionMetricsCollector() EvictionMetricsCollector

GetEvictionMetricsCollector returns the singleton evictionMetrics instance.

type WithMetrics added in v3.82.7

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

WithMetrics is a decorator that adds metrics collection to any Cache implementation.

func NewCacheWithMetrics added in v3.82.7

func NewCacheWithMetrics[T any](wrapped Cache[T], metrics BaseMetricsCollector, cacheName string) *WithMetrics[T]

NewCacheWithMetrics creates a new WithMetrics decorator that wraps the provided Cache and collects metrics using the provided BaseMetricsCollector. The cacheName parameter is used to identify the cache in the collected metrics.

func (*WithMetrics[T]) Clear added in v3.82.7

func (c *WithMetrics[T]) Clear()

Clear removes all entries from the cache. It also records a clear metric for the cache using the provided metrics collector and cache name.

func (*WithMetrics[T]) Contents added in v3.82.7

func (c *WithMetrics[T]) Contents() string

Contents returns all keys in the cache as a string. It also records a contents metric for the cache using the provided metrics collector and cache name.

func (*WithMetrics[T]) Count added in v3.82.7

func (c *WithMetrics[T]) Count() int

Count returns the number of entries in the cache. It also records a count metric for the cache using the provided metrics collector and cache name.

func (*WithMetrics[T]) Delete added in v3.82.7

func (c *WithMetrics[T]) Delete(key string)

Delete removes the value for the given key from the cache. It also records a delete metric for the cache using the provided metrics collector and cache name.

func (*WithMetrics[T]) Exists added in v3.82.7

func (c *WithMetrics[T]) Exists(key string) bool

Exists checks if the given key exists in the cache. It records a hit or miss metric for the cache using the provided metrics collector and cache name.

func (*WithMetrics[T]) Get added in v3.82.7

func (c *WithMetrics[T]) Get(key string) (T, bool)

Get retrieves the value for the given key from the underlying cache. It also records a hit or miss metric for the cache using the provided metrics collector and cache name.

func (*WithMetrics[T]) Keys added in v3.82.7

func (c *WithMetrics[T]) Keys() []string

Keys returns all keys in the cache. It also records a keys metric for the cache using the provided metrics collector and cache name.

func (*WithMetrics[T]) Set added in v3.82.7

func (c *WithMetrics[T]) Set(key string, val T)

Set sets the value for the given key in the cache. It also records a set metric for the cache using the provided metrics collector and cache name.

func (*WithMetrics[T]) Values added in v3.82.7

func (c *WithMetrics[T]) Values() []T

Values returns all values in the cache. It also records a values metric for the cache using the provided metrics collector and cache name.

Directories

Path Synopsis
Package lru provides a generic, size-limited, LRU (Least Recently Used) cache with optional metrics collection and reporting.
Package lru provides a generic, size-limited, LRU (Least Recently Used) cache with optional metrics collection and reporting.

Jump to

Keyboard shortcuts

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