ldcache

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: May 5, 2022 License: MIT Imports: 7 Imported by: 0

README

ldcache

Loading cache with Go generics 1.18+

Example of usage:

var idCache *ldcache.Cache[uuid.UUID, *User]

idCache = ldcache.NewCache(
    tcache.CacheParams[uuid.UUID, *User]{
        Loader: func(ctx context.Context, key uuid.UUID) (*User, error) {
                return UserDataFromStoreByID(key)
            },
        Size:    cacheCount,
        Expires: time.Hour * 12,
    },
)

vuser, err := idCache.Get(ctx, id)

Documentation

Overview

Package cache provides implementations of loading cache, including support for LRU and LFU.

Index

Constants

This section is empty.

Variables

View Source
var ErrNotFound = errors.New("key not found in cache")

Functions

This section is empty.

Types

type Cache

type Cache[K Key, V Value] struct {
	// contains filtered or unexported fields
}

func NewCache

func NewCache[K Key, V Value](params CacheParams[K, V]) *Cache[K, V]

func (*Cache[K, V]) Close

func (tc *Cache[K, V]) Close() error

func (*Cache[K, V]) Get

func (tc *Cache[K, V]) Get(ctx context.Context, key K) (V, error)

func (*Cache[K, V]) GetIfPresent

func (tc *Cache[K, V]) GetIfPresent(ctx context.Context, key K) (V, bool)

func (*Cache[K, V]) Invalidate

func (tc *Cache[K, V]) Invalidate(ctx context.Context, key K)

func (*Cache[K, V]) InvalidateAll

func (tc *Cache[K, V]) InvalidateAll(ctx context.Context)

func (*Cache[K, V]) Put

func (tc *Cache[K, V]) Put(ctx context.Context, key K, val V)

func (*Cache[K, V]) Refresh

func (tc *Cache[K, V]) Refresh(ctx context.Context, key K) error

func (*Cache[K, V]) SetLoader

func (tc *Cache[K, V]) SetLoader(lf LoaderFunc[K, V])

type CacheParams

type CacheParams[K Key, V Value] struct {
	Type    CacheType     // default LRU
	Size    int           // default 10000
	Expires time.Duration // default never
	Loader  LoaderFunc[K, V]
}

type CacheType

type CacheType int
const (
	LRU CacheType = iota
	LFU
)

type Func

type Func[K Key, V Value] func(K, V)

Func is a generic callback for entry events in the cache.

type Key

type Key comparable

Key is any value which is comparable.

type LoaderFunc

type LoaderFunc[K Key, V Value] func(context.Context, K) (V, error)

LoaderFunc retrieves the value corresponding to given Key. You must ensure the context is not canceled before use it in database queries!

type LoadingCache

type LoadingCache[K Key, V Value] interface {
	SomeCache[K, V]

	// Get returns value associated with Key or call underlying LoaderFunc
	// to load value if it is not present.
	Get(context.Context, K) (V, error)
	// SetLoader changes the underlying loader func
	SetLoader(LoaderFunc[K, V])
	// Refresh loads new value for Key. If the Key already existed, the previous value
	// will continue to be returned by Get while the new value is loading.
	// If Key does not exist, this function will block until the value is loaded.
	Refresh(context.Context, K) error
}

LoadingCache is a cache with values are loaded automatically and stored in the cache until either evicted or manually invalidated.

type SomeCache

type SomeCache[K Key, V Value] interface {
	// GetIfPresent returns value associated with Key or (nil, false)
	// if there is no cached value for Key.
	GetIfPresent(context.Context, K) (V, bool)

	// Put associates value with Key. If a value is already associated
	// with Key, the old one will be replaced with Value.
	Put(context.Context, K, V)

	// Invalidate discards cached value of the given Key.
	Invalidate(context.Context, K)

	// InvalidateAll discards all entries.
	InvalidateAll(context.Context)

	// Close implements io.Closer for cleaning up all resources.
	// Users must ensure the cache is not being used before closing or
	// after closed.
	Close() error
}

SomeCache is a key-value cache which entries are added and stayed in the cache until either are evicted or manually invalidated.

type Value

type Value any

Value is any value.

Jump to

Keyboard shortcuts

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