cache

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2025 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidMaxCost indicates MaxCost is not positive.
	ErrInvalidMaxCost = errors.New("MaxCost must be positive")

	// ErrInvalidNumCounters indicates NumCounters is not positive.
	ErrInvalidNumCounters = errors.New("NumCounters must be positive")

	// ErrInvalidBufferItems indicates BufferItems is not positive.
	ErrInvalidBufferItems = errors.New("BufferItems must be positive")
)

Sentinel errors for cache validation.

Functions

This section is empty.

Types

type Cache

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

Cache provides a high-performance in-memory cache

func New

func New(cfg Config, logger *zap.Logger) (*Cache, error)

New creates a new cache instance

func (*Cache) Clear

func (c *Cache) Clear()

Clear removes all entries from the cache

func (*Cache) Close

func (c *Cache) Close()

Close shuts down the cache

func (*Cache) Delete

func (c *Cache) Delete(key string)

Delete removes a value from the cache

func (*Cache) Get

func (c *Cache) Get(key string) (any, bool)

Get retrieves a value from the cache and checks TTL expiration.

This method performs both ristretto cache lookup and TTL validation. If the value has expired based on its TTL, it's automatically deleted and treated as a cache miss. The TTL check is performed atomically to prevent race conditions.

Returns the cached value and true if found and not expired, or nil and false if not found or expired.

func (*Cache) Metrics

func (c *Cache) Metrics() *ristretto.Metrics

Metrics returns cache performance metrics

func (*Cache) Set

func (c *Cache) Set(key string, value any, ttl time.Duration)

Set stores a value in the cache with TTL (time-to-live).

The value is stored with an estimated cost (64 bytes base overhead). If the cache is full and cannot evict items, the set operation may fail silently. This is by design in Ristretto to maintain performance.

TTL is tracked separately and enforced on Get() and by a background cleanup goroutine that runs every 30 seconds. Setting ttl to 0 means the value never expires (until explicitly deleted or evicted).

This method is thread-safe and can be called concurrently.

type Config

type Config struct {
	// MaxCost is the maximum cost of cache entries (in bytes approximately)
	MaxCost int64
	// NumCounters is the number of keys to track frequency
	NumCounters int64
	// BufferItems is the size of the internal buffer
	BufferItems int64
}

Config holds cache configuration

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns sensible defaults for the cache

type ValidationError added in v1.0.0

type ValidationError struct {
	Err   error
	Field string
	Value int64
}

ValidationError wraps cache configuration validation errors with context.

func (*ValidationError) Error added in v1.0.0

func (e *ValidationError) Error() string

func (*ValidationError) Unwrap added in v1.0.0

func (e *ValidationError) Unwrap() error

Jump to

Keyboard shortcuts

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