result

package
v3.5.7 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2023 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache[T any] struct {
	// contains filtered or unexported fields
}

Cache provides a means of caching value structures, along with the results of attempting to load them. An example usecase of this cache would be in wrapping a database, allowing caching of sql.ErrNoRows.

func New

func New[T any](lookups []Lookup, copy func(T) T, cap int) *Cache[T]

New returns a new initialized Cache, with given lookups, underlying value copy function and provided capacity.

func (*Cache[T]) Clear

func (c *Cache[T]) Clear()

Clear empties the cache, calling the invalidate callback where necessary.

func (*Cache[T]) Has

func (c *Cache[T]) Has(lookup string, keyParts ...any) bool

Has checks the cache for a positive result under the given lookup and key parts.

func (*Cache[T]) IgnoreErrors added in v3.2.0

func (c *Cache[T]) IgnoreErrors(ignore func(error) bool)

IgnoreErrors allows setting a function hook to determine which error types should / not be cached.

func (*Cache[T]) Invalidate

func (c *Cache[T]) Invalidate(lookup string, keyParts ...any)

Invalidate will invalidate any result from the cache found under given lookup and key parts.

func (*Cache[T]) Load

func (c *Cache[T]) Load(lookup string, load func() (T, error), keyParts ...any) (T, error)

Load will attempt to load an existing result from the cacche for the given lookup and key parts, else calling the provided load function and caching the result.

func (*Cache[T]) SetEvictionCallback

func (c *Cache[T]) SetEvictionCallback(hook func(T))

SetEvictionCallback sets the eviction callback to the provided hook.

func (*Cache[T]) SetInvalidateCallback

func (c *Cache[T]) SetInvalidateCallback(hook func(T))

SetInvalidateCallback sets the invalidate callback to the provided hook.

func (*Cache[T]) Store

func (c *Cache[T]) Store(value T, store func() error) error

Store will call the given store function, and on success store the value in the cache as a positive result.

func (*Cache[T]) Trim added in v3.5.1

func (c *Cache[T]) Trim(perc float64)

Trim ensures the cache stays within percentage of total capacity, truncating where necessary.

type Lookup added in v3.1.7

type Lookup struct {
	// Name is a period ('.') separated string
	// of struct fields this Key encompasses.
	Name string

	// AllowZero indicates whether to accept and cache
	// under zero value keys, otherwise ignore them.
	AllowZero bool

	// Multi allows specifying a key capable of storing
	// multiple results. Note this only supports invalidate.
	Multi bool
}

Lookup represents a struct object lookup method in the cache.

Jump to

Keyboard shortcuts

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