result

package
v3.3.3 Latest Latest
Warning

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

Go to latest
Published: May 14, 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[Value 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[Value any](lookups []Lookup, copy func(Value) Value, cap int) *Cache[Value]

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

func (*Cache[Value]) Clear

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

Clear empties the cache, calling the invalidate callback.

func (*Cache[Value]) Has

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

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

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

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

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

func (*Cache[Value]) Invalidate

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

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

func (*Cache[Value]) Load

func (c *Cache[Value]) Load(lookup string, load func() (Value, error), keyParts ...any) (Value, 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[Value]) SetEvictionCallback

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

SetEvictionCallback sets the eviction callback to the provided hook.

func (*Cache[Value]) SetInvalidateCallback

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

SetInvalidateCallback sets the invalidate callback to the provided hook.

func (*Cache[Value]) SetTTL

func (c *Cache[Value]) SetTTL(ttl time.Duration, update bool)

SetTTL sets the cache item TTL. Update can be specified to force updates of existing items in the cache, this will simply add the change in TTL to their current expiry time.

func (*Cache[Value]) Start

func (c *Cache[Value]) Start(freq time.Duration) bool

Start will start the cache background eviction routine with given sweep frequency. If already running or a freq <= 0 provided, this is a no-op. This will block until eviction routine started.

func (*Cache[Value]) Stop

func (c *Cache[Value]) Stop() bool

Stop will stop cache background eviction routine. If not running this is a no-op. This will block until the eviction routine has stopped.

func (*Cache[Value]) Store

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

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

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