cache

package
v1.13.0-beta.2 Latest Latest
Warning

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

Go to latest
Published: May 29, 2025 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package cache provides caching functionality with support for Redis and in-memory caching. It offers a unified interface for different caching backends with TTL support.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrKeyNotFound is returned when a key doesn't exist in the cache
	ErrKeyNotFound = errors.New("key not found")
	// ErrCacheMiss is an alias for ErrKeyNotFound for compatibility
	ErrCacheMiss = ErrKeyNotFound
	// ErrCacheFull is returned when the cache is at capacity
	ErrCacheFull = errors.New("cache is full")
	// ErrInvalidTTL is returned when an invalid TTL is provided
	ErrInvalidTTL = errors.New("invalid TTL")
)

Functions

This section is empty.

Types

type Cache

type Cache interface {
	// Get retrieves a value from the cache
	Get(ctx context.Context, key string, value any) error
	// Set stores a value in the cache with optional TTL
	Set(ctx context.Context, key string, value any, ttl time.Duration) error
	// Delete removes a value from the cache
	Delete(ctx context.Context, key string) error
	// Exists checks if a key exists in the cache
	Exists(ctx context.Context, key string) (bool, error)
	// Clear removes all values from the cache
	Clear(ctx context.Context) error
	// TTL returns the remaining TTL for a key
	TTL(ctx context.Context, key string) (time.Duration, error)
	// Keys returns all keys matching a pattern (* for all)
	Keys(ctx context.Context, pattern string) ([]string, error)
}

Cache defines the interface for cache implementations

type EvictionPolicy

type EvictionPolicy string

EvictionPolicy defines cache eviction strategies

const (
	// EvictionLRU removes least recently used items
	EvictionLRU EvictionPolicy = "lru"
	// EvictionLFU removes least frequently used items
	EvictionLFU EvictionPolicy = "lfu"
	// EvictionRandom removes random items
	EvictionRandom EvictionPolicy = "random"
)

type GobSerializer

type GobSerializer struct{}

GobSerializer serializes values using gob encoding

func (*GobSerializer) Deserialize

func (gs *GobSerializer) Deserialize(data []byte, value any) error

Deserialize converts gob bytes to a value

func (*GobSerializer) Serialize

func (gs *GobSerializer) Serialize(value any) ([]byte, error)

Serialize converts a value to gob bytes

type JSONSerializer

type JSONSerializer struct{}

JSONSerializer serializes values as JSON

func (*JSONSerializer) Deserialize

func (js *JSONSerializer) Deserialize(data []byte, value any) error

Deserialize converts JSON bytes to a value

func (*JSONSerializer) Serialize

func (js *JSONSerializer) Serialize(value any) ([]byte, error)

Serialize converts a value to JSON bytes

type LoadFunc

type LoadFunc func(ctx context.Context, key string) (any, time.Duration, error)

LoadFunc defines a function that loads a value when not in cache

type LoadingCache

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

LoadingCache wraps a cache with automatic loading

func NewLoadingCache

func NewLoadingCache(cache Cache, loadFunc LoadFunc) *LoadingCache

NewLoadingCache creates a new loading cache

func (*LoadingCache) Clear

func (lc *LoadingCache) Clear(ctx context.Context) error

Clear removes all values from the cache

func (*LoadingCache) Delete

func (lc *LoadingCache) Delete(ctx context.Context, key string) error

Delete removes a value from the cache

func (*LoadingCache) Exists

func (lc *LoadingCache) Exists(ctx context.Context, key string) (bool, error)

Exists checks if a key exists in the cache

func (*LoadingCache) Get

func (lc *LoadingCache) Get(ctx context.Context, key string, value any) error

Get retrieves a value, loading it if not present

func (*LoadingCache) Keys

func (lc *LoadingCache) Keys(ctx context.Context, pattern string) ([]string, error)

Keys returns all keys matching a pattern

func (*LoadingCache) Refresh

func (lc *LoadingCache) Refresh(ctx context.Context, key string) error

Refresh reloads a value in the cache

func (*LoadingCache) Set

func (lc *LoadingCache) Set(ctx context.Context, key string, value any, ttl time.Duration) error

Set stores a value in the cache

func (*LoadingCache) TTL

func (lc *LoadingCache) TTL(ctx context.Context, key string) (time.Duration, error)

TTL returns the remaining TTL for a key

type MemoryCache

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

MemoryCache is an in-memory cache implementation

func NewMemoryCache

func NewMemoryCache(opts ...MemoryCacheOption) *MemoryCache

NewMemoryCache creates a new in-memory cache

func (*MemoryCache) Clear

func (c *MemoryCache) Clear(_ context.Context) error

Clear removes all values from the cache

func (*MemoryCache) Delete

func (c *MemoryCache) Delete(_ context.Context, key string) error

Delete removes a value from the cache

func (*MemoryCache) Exists

func (c *MemoryCache) Exists(_ context.Context, key string) (bool, error)

Exists checks if a key exists in the cache

func (*MemoryCache) Get

func (c *MemoryCache) Get(_ context.Context, key string, value any) error

Get retrieves a value from the cache

func (*MemoryCache) GetMultiple

func (c *MemoryCache) GetMultiple(_ context.Context, keys []string) (map[string]any, error)

GetMultiple retrieves multiple values from the cache

func (*MemoryCache) Keys

func (c *MemoryCache) Keys(_ context.Context, pattern string) ([]string, error)

Keys returns all keys matching a pattern (* for all)

func (*MemoryCache) Set

func (c *MemoryCache) Set(_ context.Context, key string, value any, ttl time.Duration) error

Set stores a value in the cache with optional TTL

func (*MemoryCache) SetMultiple

func (c *MemoryCache) SetMultiple(_ context.Context, items map[string]any, ttl time.Duration) error

SetMultiple stores multiple values in the cache

func (*MemoryCache) Stop

func (c *MemoryCache) Stop()

Stop stops the cleanup goroutine

func (*MemoryCache) TTL

func (c *MemoryCache) TTL(_ context.Context, key string) (time.Duration, error)

TTL returns the remaining TTL for a key

type MemoryCacheOption

type MemoryCacheOption func(*MemoryCache)

MemoryCacheOption configures a MemoryCache

func WithCleanupInterval

func WithCleanupInterval(interval time.Duration) MemoryCacheOption

WithCleanupInterval sets the interval for cleaning expired entries

func WithEvictionPolicy

func WithEvictionPolicy(policy EvictionPolicy) MemoryCacheOption

WithEvictionPolicy sets the eviction policy

func WithMaxSize

func WithMaxSize(size int) MemoryCacheOption

WithMaxSize sets the maximum number of entries

func WithMetrics

func WithMetrics(metrics Metrics) MemoryCacheOption

WithMetrics sets the metrics collector

type Metrics

type Metrics interface {
	IncrHits(cache string)
	IncrMisses(cache string)
	IncrSets(cache string)
	IncrDeletes(cache string)
	IncrEvictions(cache string)
	ObserveSize(cache string, size int)
}

Metrics defines the interface for cache metrics

type NamespaceCache

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

NamespaceCache wraps a cache with namespace support

func NewNamespaceCache

func NewNamespaceCache(cache Cache, namespace string) *NamespaceCache

NewNamespaceCache creates a new namespace cache

func (*NamespaceCache) Clear

func (nc *NamespaceCache) Clear(ctx context.Context) error

Clear removes all values from the namespace

func (*NamespaceCache) Delete

func (nc *NamespaceCache) Delete(ctx context.Context, key string) error

Delete removes a value from the cache

func (*NamespaceCache) Exists

func (nc *NamespaceCache) Exists(ctx context.Context, key string) (bool, error)

Exists checks if a key exists in the cache

func (*NamespaceCache) Get

func (nc *NamespaceCache) Get(ctx context.Context, key string, value any) error

Get retrieves a value from the cache

func (*NamespaceCache) Keys

func (nc *NamespaceCache) Keys(ctx context.Context, pattern string) ([]string, error)

Keys returns all keys matching a pattern in the namespace

func (*NamespaceCache) Set

func (nc *NamespaceCache) Set(ctx context.Context, key string, value any, ttl time.Duration) error

Set stores a value in the cache

func (*NamespaceCache) TTL

func (nc *NamespaceCache) TTL(ctx context.Context, key string) (time.Duration, error)

TTL returns the remaining TTL for a key

type Serializer

type Serializer interface {
	Serialize(value any) ([]byte, error)
	Deserialize(data []byte, value any) error
}

Serializer defines the interface for serializing cache values

Jump to

Keyboard shortcuts

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