cache

package
v1.31.0 Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2024 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache interface {
	// Get returns the value for the given key in the cache, if it exists.
	Get(key any) (any, bool)

	// Set sets a value for the key in the cache, with the given cost.
	Set(key, entry any, cost int64) bool

	// Wait waits for the cache to process and apply updates.
	Wait()

	// Close closes the cache's background workers (if any).
	Close()

	// GetMetrics returns the metrics block for the cache.
	GetMetrics() Metrics

	zerolog.LogObjectMarshaler
}

Cache defines an interface for a generic cache.

func NewCache

func NewCache(config *Config) (Cache, error)

NewCache creates a new ristretto cache from the given config.

func NewCacheWithMetrics added in v1.17.0

func NewCacheWithMetrics(name string, config *Config) (Cache, error)

NewCacheWithMetrics creates a new ristretto cache from the given config that also reports metrics to the default Prometheus registry.

func NoopCache added in v1.11.0

func NoopCache() Cache

NoopCache returns a cache that does nothing.

type Config

type Config struct {
	// NumCounters determines the number of counters (keys) to keep that hold
	// access frequency information. It's generally a good idea to have more
	// counters than the max cache capacity, as this will improve eviction
	// accuracy and subsequent hit ratios.
	//
	// For example, if you expect your cache to hold 1,000,000 items when full,
	// NumCounters should be 10,000,000 (10x). Each counter takes up roughly
	// 3 bytes (4 bits for each counter * 4 copies plus about a byte per
	// counter for the bloom filter). Note that the number of counters is
	// internally rounded up to the nearest power of 2, so the space usage
	// may be a little larger than 3 bytes * NumCounters.
	NumCounters int64

	// MaxCost can be considered as the cache capacity, in whatever units you
	// choose to use.
	//
	// For example, if you want the cache to have a max capacity of 100MB, you
	// would set MaxCost to 100,000,000 and pass an item's number of bytes as
	// the `cost` parameter for calls to Set. If new items are accepted, the
	// eviction process will take care of making room for the new item and not
	// overflowing the MaxCost value.
	MaxCost int64

	// DefaultTTL configures a default deadline on the lifetime of any keys set
	// to the cache.
	DefaultTTL time.Duration
}

Config for caching. See: https://github.com/outcaste-io/ristretto#Config

func (*Config) MarshalZerologObject added in v1.13.0

func (c *Config) MarshalZerologObject(e *zerolog.Event)

type Metrics

type Metrics interface {
	// Hits is the number of cache hits.
	Hits() uint64

	// Misses is the number of cache misses.
	Misses() uint64

	// CostAdded returns the total cost of added items.
	CostAdded() uint64

	// CostEvicted returns the total cost of evicted items.
	CostEvicted() uint64
}

Metrics defines metrics exported by the cache.

Jump to

Keyboard shortcuts

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