cache

package
v0.37.2 Latest Latest
Warning

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

Go to latest
Published: Aug 20, 2026 License: MIT Imports: 15 Imported by: 8

Documentation

Overview

Package cache provides a unified caching layer with memory and Redis backends.

Index

Constants

View Source
const (
	InstrumentationStart  = "cache-start"
	InstrumentationClose  = "cache-close"
	InstrumentationPing   = "cache-ping"
	InstrumentationGet    = "cache-get"
	InstrumentationGetHit = "cache-get-hit"
	InstrumentationLoader = "cache-loader"
	InstrumentationSet    = "cache-set"
	InstrumentationDelete = "cache-delete"
)

Instrumentation operation names for cache events.

Variables

View Source
var ErrCacheClosed = errors.New("cache closed")

ErrCacheClosed is returned when an operation is attempted on a closed cache.

View Source
var ErrCacheUnavailable = errors.New("cache connection unavailable")

ErrCacheUnavailable is returned when the cache backend connection is not established or is lost.

Functions

func InstrDelete added in v0.15.0

func InstrDelete(op string, args ...any) (string, bool)

InstrDelete returns cache key if the operation is cache delete event.

func InstrGet added in v0.15.0

func InstrGet(op string, args ...any) (string, bool)

InstrGet returns cache key if the operation is cache get event.

func InstrGetHit added in v0.37.0

func InstrGetHit(op string, args ...any) (string, bool)

InstrGetHit returns cache key if the operation is cache get hit event.

func InstrInstance added in v0.37.1

func InstrInstance(args ...any) (string, bool)

InstrInstance returns the cache instance name from instrumentation event arguments if present.

func InstrLoader added in v0.15.0

func InstrLoader(op string, args ...any) (string, bool)

InstrLoader returns cache key if the operation is cache loader event.

func InstrSet added in v0.15.0

func InstrSet(op string, args ...any) (string, bool)

InstrSet returns cache key if the operation is cache set event.

func ValidateConnectionString

func ValidateConnectionString(typ Type, connStr string) error

ValidateConnectionString validates connection string for specific cache type.

Types

type Cache

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

Cache represents a cache.

func New

func New(opts ...Option) *Cache

New creates a new cache with specified type.

func (*Cache) Close

func (c *Cache) Close()

Close cache and all its instances.

func (*Cache) ConfiguredKeyPrefix added in v0.33.0

func (c *Cache) ConfiguredKeyPrefix() string

ConfiguredKeyPrefix returns the global key prefix the cache was created with.

func (*Cache) ConfiguredType added in v0.33.0

func (c *Cache) ConfiguredType() Type

ConfiguredType returns the cache type the cache was created with.

func (*Cache) Connection added in v0.33.0

func (c *Cache) Connection() valkey.Client

Connection returns the underlying Redis client shared by the cache or nil if the connection is not established.

func (*Cache) Ping

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

Ping cache and all its instances.

func (*Cache) Start

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

Start cache.

An unreachable backend does not fail the start; the connection is retried in the background and operations return ErrCacheUnavailable until it is established.

type ClientCache added in v0.37.0

type ClientCache bool

ClientCache enables or disables server-assisted client-side caching support.

type ClientCacheSize added in v0.37.0

type ClientCacheSize int

ClientCacheSize sets the client-side cache memory in bytes per Redis connection.

type ClientCacheTTL added in v0.37.0

type ClientCacheTTL time.Duration

ClientCacheTTL sets the local cache TTL for values read through Redis-backed cache instances.

type ConnectionPassword

type ConnectionPassword string

ConnectionPassword is a connection password for the cache instance.

type ConnectionString

type ConnectionString string

ConnectionString is a connection string for the cache instance.

type DefaultTTL

type DefaultTTL time.Duration

DefaultTTL is an default TTL for items in cache instance.

type Instance added in v0.15.0

type Instance[T any] interface {
	// Get value from cache. If value is not found, it will return default value.
	Get(ctx context.Context, key string, opts ...ItemOption[T]) (T, error)
	// Pop returns value from tha cache and deletes it. If value is not found, it will return ErrKeyNotFound error.
	Pop(ctx context.Context, key string) (T, error)
	// Set value in cache.
	Set(ctx context.Context, key string, value T, opts ...ItemOption[T]) error
	// Delete value from cache.
	Delete(ctx context.Context, key string) error
}

Instance of a cache.

func Create

func Create[T any](cache *Cache, name string, opts ...Option) (Instance[T], error)

Create new cache instance with specified name and options.

func Get

func Get[T any](cache *Cache, name string) (Instance[T], error)

Get returns pre-configured cache instance by name.

type InstanceCloser added in v0.15.0

type InstanceCloser interface {
	// Close cache instance.
	Close()
}

InstanceCloser represents a cache instance close method.

type InstancePinger added in v0.15.0

type InstancePinger interface {
	Ping(ctx context.Context) error
}

InstancePinger represents a cache instance ping method.

type Instrumenter added in v0.7.0

type Instrumenter instrumenter.Instrumenter

Instrumenter is a function that instruments cache operations.

type ItemOption

type ItemOption[T any] interface {
	// contains filtered or unexported methods
}

ItemOption is an option for the cached item.

type KeyNotFoundError added in v0.15.0

type KeyNotFoundError struct {
	Key string
}

KeyNotFoundError is returned when a cache key is not found.

func (KeyNotFoundError) Error added in v0.15.0

func (e KeyNotFoundError) Error() string

type KeyPrefix

type KeyPrefix string

KeyPrefix is a prefix for the cache keys.

type Loader

type Loader func(ctx context.Context, key string) (any, error)

Loader is a function that loads data when cache key is missing.

WARNING: it's not guaranteed that the function will be called only once.

type Option added in v0.15.0

type Option interface {
	// contains filtered or unexported methods
}

Option for the cache instance.

type Serialize added in v0.30.0

type Serialize bool

Serialize controls whether values are JSON-serialized before storage in the memory cache. Serialization is enabled by default to guarantee that values backed by unsafe byte-to-string conversions (e.g. from fasthttp buffers) are safely copied before the underlying buffer is reused.

Set Serialize(false) only when using the memory cache explicitly with types that cannot be JSON-serialized (e.g. structs containing channels or functions). Has no effect on Redis-backed caches which always serialize.

type TTL

type TTL[T any] time.Duration

TTL represents time to keep item in cache.

type Type added in v0.15.0

type Type string

Type of a cache.

const (
	// MemoryCache store data in memory.
	MemoryCache Type = "memory"
	// RedisCache store data in Redis database.
	RedisCache Type = "redis"
	// RedisClusterCache store data in Redis database cluster.
	RedisClusterCache Type = "redis-cluster"
	// RedisSentinelCache store data in Redis database with sentinel.
	RedisSentinelCache Type = "redis-sentinel"
)

func InstrBackend added in v0.37.0

func InstrBackend(args ...any) (Type, bool)

InstrBackend returns the cache backend type from instrumentation event arguments if present.

Jump to

Keyboard shortcuts

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