cache

package
v0.34.0 Latest Latest
Warning

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

Go to latest
Published: Jun 19, 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"
	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.

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 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 ParseRedisClusterURL added in v0.11.0

func ParseRedisClusterURL(v string) (*redis.ClusterOptions, error)

ParseRedisClusterURL parses a Redis Cluster URL string into cluster client options.

func ParseRedisSentinelURL added in v0.19.0

func ParseRedisSentinelURL(urlStr string) (*redis.FailoverOptions, error)

ParseRedisSentinelURL parses Redis Sentinel URL to extract connection information.

func ParseRedisURL added in v0.11.0

func ParseRedisURL(v string) (*redis.Options, error)

ParseRedisURL parses a Redis URL string into client options.

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() redis.Cmdable

Connection returns the underlying Redis connection shared by the cache.

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.

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 Logger added in v0.30.0

type Logger struct{ *zap.Logger }

Logger sets the logger for the cache backends. When provided, internal backend log messages are forwarded to it; the zap logger's own level configuration controls what is actually emitted.

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"
)

Jump to

Keyboard shortcuts

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