cache

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2025 License: BSD-3-Clause-Clear Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrCacheMiss = errors.New("cache miss")
)

Functions

func EstimateRistrettoConfigParams

func EstimateRistrettoConfigParams(maxCost int64) (int64, int64, error)

EstimateRistrettoConfigParams estimates Ristretto cache config parameters Uses a conservative default average item cost (1KB) if the true average is unknown.

Types

type Cache

type Cache struct {
	// contains filtered or unexported fields
}

Cache is a cache implementation using gocache for any value type.

func TestCacheClient

func TestCacheClient(expiration time.Duration) (*Cache, error)

TestCacheClient creates a test cache client with predefined options.

func (*Cache) Delete

func (c *Cache) Delete(ctx context.Context, key string) error

func (*Cache) Get

func (c *Cache) Get(ctx context.Context, key string) (any, error)

Get retrieves a value from the cache

func (*Cache) Invalidate

func (c *Cache) Invalidate(ctx context.Context) error

func (*Cache) Set

func (c *Cache) Set(ctx context.Context, key string, object any, tags []string) error

Set stores a value of type T in the cache.

type Config

type Config struct {
	Driver         string          `mapstructure:"driver" json:"driver" default:"ristretto"`
	RistrettoCache RistrettoConfig `mapstructure:"ristretto" json:"ristretto"`
}

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

Manager is a cache manager for any value.

func NewCacheManager

func NewCacheManager(maxCost int64) (*Manager, error)

NewCacheManager creates a new cache manager using Ristretto as the backend.

func (*Manager) Close

func (c *Manager) Close()

func (*Manager) NewCache

func (c *Manager) NewCache(serviceName string, log *logger.Logger, options Options) (*Cache, error)

NewCache creates a new Cache client instance with the given service name and options. The purpose of this function is to create a new cache for a specific service. Because caching can be expensive we want to make sure there are some strict controls with how it is used.

type Options

type Options struct {
	Expiration time.Duration
}

type RistrettoConfig

type RistrettoConfig struct {
	// MaxCost is the maximum cost of the cache, can be a number (bytes) or a string like "1gb"
	MaxCost string `mapstructure:"max_cost" json:"max_cost" default:"1gb"`
}

CacheRistrettoConfig supports human-friendly size strings like "1gb", "512mb", etc.

func (RistrettoConfig) MaxCostBytes

func (c RistrettoConfig) MaxCostBytes() int64

MaxCostBytes parses MaxCost and returns the value in bytes. Supports suffixes: b, kb, mb, gb, tb (case-insensitive).

Jump to

Keyboard shortcuts

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