Help us shape the future of Go! Take the 2026 Go Developer Survey

cache

package
v0.9.82 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const NoCache = time.Duration(0)

NoCache is passed as TTL to indicate that no expiration should be set.

Variables

View Source
var ErrSetIfAbsentUnsupported = errors.New("cache does not support atomic set-if-absent")

Functions

func GetDecode

func GetDecode[T any](ctx context.Context, cache Getter, key string) (T, error)

GetDecode retrieves a value of type T from the cache, decoding it from gob format. If the value is not found, it returns the zero value of T and no error. Best to use pointer to T, to distinguish between "not found" and "found but empty" cases.

func SetEncode

func SetEncode[T any](ctx context.Context, cache Setter, key string, value T, ttl time.Duration) error

SetEncode serializes a value of type T using gob encoding and stores it in the cache. Value should be passed by value, not by pointer.

Types

type Cache

type Cache interface {
	Getter
	Setter
	SetIfAbsent(ctx context.Context, key string, value string, ttl time.Duration) (stored bool, err error)
	// AllowRateLimit consumes one token, refilling one token per interval up to burst.
	AllowRateLimit(
		ctx context.Context, key string, interval time.Duration, burst int, ttl time.Duration,
	) (allowed bool, err error)
	Invalidate(ctx context.Context, key string) error
}

type Engine

type Engine interface {
	Cache
	Ping(ctx context.Context) error
	Shutdown(ctx context.Context) error
}

type Getter

type Getter interface {
	Get(ctx context.Context, key string) (value string, found bool, err error)
}

type NoopCache

type NoopCache struct{}

NoopCache is a no-op implementation of Cache intended for tests.

func NewNoop

func NewNoop() *NoopCache

func (NoopCache) AllowRateLimit

func (NoopCache) AllowRateLimit(
	_ context.Context,
	_ string,
	_ time.Duration,
	_ int,
	_ time.Duration,
) (bool, error)

func (NoopCache) Get

func (NoopCache) Invalidate

func (NoopCache) Invalidate(_ context.Context, _ string) error

func (NoopCache) Ping

func (NoopCache) Ping(ctx context.Context) error

func (NoopCache) Set

func (NoopCache) Set(_ context.Context, _, _ string, _ time.Duration) error

func (NoopCache) SetIfAbsent

func (NoopCache) SetIfAbsent(
	_ context.Context,
	_, _ string,
	_ time.Duration,
) (bool, error)

func (NoopCache) Shutdown

func (NoopCache) Shutdown(_ context.Context) error

type Setter

type Setter interface {
	Set(ctx context.Context, key string, value string, ttl time.Duration) error
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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