cache

package module
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2025 License: MIT Imports: 5 Imported by: 0

README

cache

Simple cache with using different libraries.

go get github.com/worldline-go/cache

Usage

priceCache, err := cache.New[string, int](ctx,
    memory.Store,
    cache.WithMaxItems(100),
    cache.WithTTL(60 * time.Second),
)

err := priceCache.Set(ctx, "key", 100)
v, ok, err := vcache.Get(ctx, "key")

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrStoreNotExist = errors.New("store does not exist")

Functions

This section is empty.

Types

type Cacher

type Cacher[K comparable, V any] interface {
	Get(ctx context.Context, key K) (V, bool, error)
	Set(ctx context.Context, key K, value V) error
}

func New

func New[K comparable, V any, C any](ctx context.Context, store Store[K, V, C], opts ...Option) (Cacher[K, V], error)

type Config

type Config struct {
	// The maximum number of items the cache can hold.
	MaxItems int `cfg:"max_items" json:"max_items"`
	// TTL is the time to live for each item in the cache.
	TTL time.Duration `cfg:"ttl" json:"ttl"`
	// Store is the configuration for the store.
	Store any `cfg:"store" json:"store"`
}

func (*Config) SetDefault

func (c *Config) SetDefault(maxItems int, ttl time.Duration)

func (*Config) ToOption

func (c *Config) ToOption() Option

type Option

type Option func(*option)

func WithMaxItems

func WithMaxItems(maxItems int) Option

WithMaxItems sets the maximum number of items the cache can hold.

func WithStoreConfig added in v0.2.0

func WithStoreConfig(v any) Option

func WithTTL

func WithTTL(ttl time.Duration) Option

WithTTL sets the time to live for each item in the cache.

type Store added in v0.2.0

type Store[K comparable, V any, C any] func(ctx context.Context, config C) (Cacher[K, V], error)

Directories

Path Synopsis
store

Jump to

Keyboard shortcuts

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