cacheutils

package
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2024 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewMemCache

func NewMemCache(opts ...CacheOptions) *cache

Types

type CacheOption

type CacheOption struct {
	Heartbeat time.Duration
	LoadRetry int
}

type CacheOptions

type CacheOptions func(opt *CacheOption)

type Key

type Key interface {
	// Hash is used for internal mapping. It has to be unique and non-pointer
	Hash() interface{}
}

type LoadFunc

type LoadFunc func(ctx context.Context, k Key) (v interface{}, exp time.Time, err error)

type MemCache

type MemCache interface {
	// GetOrLoad try to get cached entry, using provided validator to check the entry, if not valid, try to load it.
	// If there is any error during load, it's cached and returned from this method
	// Note: this method is the main method for this in-memory cache
	GetOrLoad(ctx context.Context, k Key, loader LoadFunc, validator ValidateFunc) (interface{}, error)
	// Update Utility method, force change the loaded entry's value.
	// If given key doesn't exist or is invalidated, this function does nothing and return false, otherwise returns true.
	// If there is any error during update, it's cached and returned from this method
	// If this Update is used while the entry is loading, it will wait until loading finishes and then perform update
	Update(ctx context.Context, k Key, updater UpdateFunc) (bool, error)
	// Delete Utility method, remove the cached entry of given key, regardless if it's valid
	Delete(k Key)
	// Reset Utility method, reset the cache and remove all entries, regardless if they are valid
	Reset()
	// Evict Utility method, cleanup the cache, removing any invalid entries, free up memory
	// Note: this process is also performed periodically, normally there is no need to call this function manually
	Evict()
}

type StringKey

type StringKey string

func (StringKey) Hash

func (s StringKey) Hash() interface{}

func (StringKey) String

func (s StringKey) String() string

type UpdateFunc

type UpdateFunc func(ctx context.Context, k Key, old interface{}) (v interface{}, exp time.Time, err error)

type ValidateFunc

type ValidateFunc func(ctx context.Context, v interface{}) bool

Jump to

Keyboard shortcuts

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