cache

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 28, 2025 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetEx

func GetEx[T any](ctx context.Context, c Cache[T], key string, builder FetchCached[T], options ...Option) (T, bool, error)

GetEx retrieves an object from the cache using the provided key. And returns the object, a boolean indicating if it was found, and an error if any occurred. If the object is not found, it uses the builder function to create the object. When the builder returns an error, the cache will not be set. and found will be false.

func GetJson

func GetJson[T any](ctx context.Context, c ByteCache, key string, options ...Option) (T, bool, error)

func GetJsonEx

func GetJsonEx[T any](ctx context.Context, c ByteCache, key string, builder FetchCached[T], options ...Option) (T, bool, error)

GetJsonEx retrieves a JSON object from the cache using the provided key. Similar to GetObjectEx, but specifically for JSON data.

func GetObject

func GetObject[T any, D codec.Decoder](ctx context.Context, c ByteCache, d D, key string) (T, bool, error)

func GetObjectEx

func GetObjectEx[T any, D codec.Decoder, E codec.Encoder](ctx context.Context, c ByteCache, d D, e E, key string, builder FetchCached[T], options ...Option) (T, bool, error)

GetObjectEx retrieves an object from the cache using the provided key. Something like GetEx, but for ByteCache.

func IsZero

func IsZero[T any](t T) bool

func Set

func Set[T any](ctx context.Context, c Cache[T], key string, value T, options ...Option) error

func SetJson

func SetJson[T any](ctx context.Context, c ByteCache, key string, value T, options ...Option) error

func SetObject

func SetObject[T any, E codec.Encoder](ctx context.Context, c ByteCache, e E, key string, value T, options ...Option) error

Types

type ByteCache

type ByteCache Cache[[]byte]

type Cache

type Cache[S any] interface {
	Set(ctx context.Context, key string, val S) error
	SetWithTTL(ctx context.Context, key string, val S, expiration time.Duration) error
	MultiSet(ctx context.Context, valMap map[string]S) error
	MultiSetWithTTL(ctx context.Context, valMap map[string]S, expiration time.Duration) error

	Get(ctx context.Context, key string) (S, bool, error)
	MultiGet(ctx context.Context, keys []string) (map[string]S, error)

	Del(ctx context.Context, key string) error
	MultiDel(ctx context.Context, keys []string) error
	DelAll(ctx context.Context) error

	Exists(ctx context.Context, key string) (bool, error)

	io.Closer
}

type FetchCached

type FetchCached[T any] = func() (obj T, err error)

FetchCached is a function type that defines a builder for fetching cached objects. It should return the object of type T and an error if any occurs during the fetching process. If error is nil, it indicates that the object was successfully fetched or built. So cache can be set. If the object is not found or cannot be built, it should return a zero value of type T and an error. Then the cache will not be set.

type Option

type Option func(o *options)

func WithDynamicTTL

func WithDynamicTTL[T any](calculator func(value T) (bool, time.Duration)) Option

WithDynamicTTL allows setting a dynamic TTL based on the value type T. The calculator function should return a boolean indicating whether the TTL is set, and the duration for which the value should be cached.

func WithExpiration

func WithExpiration(expiration time.Duration) Option

func WithNeverExpire

func WithNeverExpire() Option

func WithSingleflight

func WithSingleflight(single *singleflight.Group) Option

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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