cache

package
v0.3.6 Latest Latest
Warning

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

Go to latest
Published: Mar 20, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const Lens scene.InfraName = "cache"

Variables

View Source
var (
	ErrInvalidCacheClient = _eg.CreateError(0, "invalid cache client")
	ErrInvalidCacheKey    = _eg.CreateError(1, "invalid cache key")
	ErrInvalidLoader      = _eg.CreateError(2, "invalid cache loader")
	ErrCacheRead          = _eg.CreateError(3, "cache read failed")
	ErrCacheWrite         = _eg.CreateError(4, "cache write failed")
	ErrCacheDelete        = _eg.CreateError(5, "cache delete failed")
	ErrCacheEncode        = _eg.CreateError(6, "cache encode failed")
	ErrCacheDecode        = _eg.CreateError(7, "cache decode failed")
)
View Source
var NoExpiration = time.Duration(-1)

Functions

func Delete added in v0.3.6

func Delete(ctx context.Context, client *Client, keys ...string) error

func GetOrLoad added in v0.3.6

func GetOrLoad[T any](
	ctx context.Context,
	client *Client,
	key string,
	policy GetOrLoadPolicy[T],
	load Loader[T],
) (T, error)

GetOrLoad returns cached value first. On miss, it loads from source once per key (singleflight), writes it into cache, and returns the loaded value.

Types

type Client added in v0.3.6

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

func NewClient added in v0.3.6

func NewClient(store ICache, opts ...ClientOption) *Client

type ClientOption added in v0.3.6

type ClientOption func(*Client)

func WithCodec added in v0.3.6

func WithCodec(codec Codec) ClientOption

func WithMetrics added in v0.3.6

func WithMetrics(metrics Metrics) ClientOption

func WithTTLJitter added in v0.3.6

func WithTTLJitter(ratio float64) ClientOption

WithTTLJitter configures random ttl jitter ratio in [0, 1]. Example: 0.1 means final ttl is randomized in [0.9x, 1.1x].

type Codec added in v0.3.6

type Codec interface {
	Marshal(v any) ([]byte, error)
	Unmarshal(data []byte, dst any) error
}

type GetOrLoadPolicy added in v0.3.6

type GetOrLoadPolicy[T any] struct {
	TTL       time.Duration
	Tags      []string
	Cacheable func(v T) bool
}

type ICache

type ICache interface {
	scene.Named
	Get(ctx context.Context, key string) (value []byte, hit bool, err error)
	Set(ctx context.Context, key string, value []byte, expiration time.Duration, tags ...string) error
	Delete(ctx context.Context, keys ...string) error
	InvalidateTags(ctx context.Context, tags ...string) error
}

type JSONCodec added in v0.3.6

type JSONCodec struct{}

func (JSONCodec) Marshal added in v0.3.6

func (JSONCodec) Marshal(v any) ([]byte, error)

func (JSONCodec) Unmarshal added in v0.3.6

func (JSONCodec) Unmarshal(data []byte, dst any) error

type Loader added in v0.3.6

type Loader[T any] func(ctx context.Context) (T, error)

type Metrics added in v0.3.6

type Metrics interface {
	RecordGet(hit bool, d time.Duration, err error)
	RecordLoad(d time.Duration, err error)
	RecordSet(d time.Duration, err error)
	RecordDelete(d time.Duration, err error)
	RecordDecodeError(err error)
}

type NopMetrics added in v0.3.6

type NopMetrics struct{}

func (NopMetrics) RecordDecodeError added in v0.3.6

func (NopMetrics) RecordDecodeError(error)

func (NopMetrics) RecordDelete added in v0.3.6

func (NopMetrics) RecordDelete(time.Duration, error)

func (NopMetrics) RecordGet added in v0.3.6

func (NopMetrics) RecordGet(bool, time.Duration, error)

func (NopMetrics) RecordLoad added in v0.3.6

func (NopMetrics) RecordLoad(time.Duration, error)

func (NopMetrics) RecordSet added in v0.3.6

func (NopMetrics) RecordSet(time.Duration, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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