cache

package
v0.27.0 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

This file implements a no-op cache that always misses and never stores values. NoCache is useful for testing or when caching should be disabled without changing code.

This file implements a high-performance cache using Ristretto v2. It provides thread-safe caching with singleflight to prevent duplicate loads.

This file adapts fabric-smart-client's generic second-chance cache.Map to the Get/Add/Delete/GetOrLoad shape this package's cache consumers expect, the same shape the fabric-smart-client secondcache package exposed before it was removed upstream.

Index

Constants

View Source
const (
	// ZeroCost with this ristretto uses the Cost function defined in its configuration
	ZeroCost = 0

	// Default Ristretto configuration values
	DefaultNumCounters = 1e6 // 1 million
	DefaultMaxCost     = 1e8 // 100 million
	DefaultBufferItems = 64
)

Variables

This section is empty.

Functions

func NewDefaultRistrettoCache

func NewDefaultRistrettoCache[T any]() (*ristrettoCache[T], error)

func NewRistrettoCache

func NewRistrettoCache[T any](config *ristretto.Config[string, T]) (*ristrettoCache[T], error)

NewRistrettoCache creates and returns a new ristretto-based cache implementation.

func NewRistrettoCacheWithSize

func NewRistrettoCacheWithSize[T any](maxCost int64) (*ristrettoCache[T], error)

Types

type Config

type Config[K ristretto.Key, V any] = ristretto.Config[K, V]

Config is a shortcut for the ristretto Configuration.

type NoCache

type NoCache[T any] struct {
}

NoCache implements a dummy cache that does nothing

func NewNoCache

func NewNoCache[T any]() *NoCache[T]

NewNoCache returns a new instance of NoCache

func (*NoCache[T]) Add

func (n *NoCache[T]) Add(key string, value T)

func (*NoCache[T]) Delete

func (n *NoCache[T]) Delete(key string)

func (*NoCache[T]) Get

func (n *NoCache[T]) Get(key string) (T, bool)

func (*NoCache[T]) GetOrLoad

func (n *NoCache[T]) GetOrLoad(key string, loader func() (T, error)) (T, bool, error)

type SecondChanceCache added in v0.26.0

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

SecondChanceCache adapts fabric-smart-client's cache.Map to the Get/Add/Delete/GetOrLoad shape used by this package's consumers.

func NewSecondChanceCache added in v0.26.0

func NewSecondChanceCache[T any](size, buffer int) *SecondChanceCache[T]

NewSecondChanceCache returns a cache holding at most size entries, evicting buffer of them per victim scan once full. See fabric-smart-client's cache.NewSecondChanceCache for the eviction policy.

func (*SecondChanceCache[T]) Add added in v0.26.0

func (c *SecondChanceCache[T]) Add(key string, value T)

func (*SecondChanceCache[T]) Delete added in v0.26.0

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

func (*SecondChanceCache[T]) Get added in v0.26.0

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

func (*SecondChanceCache[T]) GetOrLoad added in v0.26.0

func (c *SecondChanceCache[T]) GetOrLoad(key string, loader func() (T, error)) (T, bool, error)

GetOrLoad returns the cached value for key, or loads it via loader on a miss. Concurrent misses for the same key are collapsed via singleflight so loader runs once.

Jump to

Keyboard shortcuts

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