cache

package
v1.89.6 Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2026 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[T any] interface {
	Get(ctx context.Context, key string) (T, bool, error)
	Set(ctx context.Context, key string, value T) error
	Delete(ctx context.Context, key string) error
	Purge(ctx context.Context) error
}

Cache is a generic cache interface with TTL-based expiration. TTL is configured at construction time, not per-operation.

func New

func New[T any](opts ...Option) (Cache[T], error)

New creates a new Cache[T]. If NATS options are provided and the connection is non-nil, a NATS KV backend is returned. Otherwise an in-memory LRU is used.

type Logger

type Logger interface {
	Debugw(msg string, keyvals ...any)
	Warnw(msg string, keyvals ...any)
	Infow(msg string, keyvals ...any)
	Errorw(msg string, keyvals ...any)
}

Logger is a structured logging interface compatible with Kratos log.Helper but does not import it.

type Option

type Option func(*config)

Option configures cache construction.

func WithDescription

func WithDescription(desc string) Option

WithDescription sets the NATS KV bucket description. Ignored for in-memory backend.

func WithLogger

func WithLogger(l Logger) Option

WithLogger sets a structured logger for cache operations.

func WithNATS

func WithNATS(conn *nats.Conn, bucketName string) Option

WithNATS enables the NATS JetStream KV backend.

func WithReconnect

func WithReconnect(ch <-chan struct{}) Option

WithReconnect provides a channel that signals NATS reconnection events.

func WithTTL

func WithTTL(d time.Duration) Option

WithTTL sets the expiration duration for cache entries. Required.

Jump to

Keyboard shortcuts

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