cache

package module
v0.3.3 Latest Latest
Warning

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

Go to latest
Published: May 5, 2026 License: 0BSD Imports: 4 Imported by: 0

README

cache 🔗

License Coverage GitHub Workflow Status Go Report Card Go PKG

Simple cache with using different libraries.

go get github.com/rakunlabs/cache

Usage

Result of cache get, set and delete methods; stores could be force to types.

Get(ctx context.Context, key K) (V, bool, error)
Set(ctx context.Context, key K, value V) error
Delete(ctx context.Context, key K) error

Additonal function for help get-set together.

GetSet(ctx context.Context, key K, fn func() (V, error)) (V, error)
memory

To not use max items and ttl, you can pass empty config cache.WithStoreConfig(&memory.Config{}).

priceCache, err := cache.New[string, int](ctx,
    memory.Store,
    cache.WithStoreConfig(&memory.Config{
        MaxItems: 100,
        TTL:      10 * time.Minute,
    }),
)

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

Get a redis client and give it to the cache.

redisClient, err := connredis.New(connredis.Config{
    Address: s.container.Address(),
})
if err != nil {
    // handle error
}

c, err := cache.New(s.T().Context(),
    redis.Store(redisClient),
    cache.WithStoreConfig(redis.Config{
        TTL: 3 * time.Second,
    }),
)
if err != nil {
    // handle error
}

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 Cache

type Cache[K comparable, V any] struct {
	Cacher[K, V]
	// contains filtered or unexported fields
}

func New

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

func (*Cache[K, V]) GetSet

func (c *Cache[K, V]) GetSet(ctx context.Context, key K, fn func() (V, error)) (V, error)

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
	Delete(ctx context.Context, key K) error
}

type Option

type Option[T any] func(*option[T])

func WithStoreConfig

func WithStoreConfig[T any](cfg T) Option[T]

type Store

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

Directories

Path Synopsis
store
redis module

Jump to

Keyboard shortcuts

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