Documentation ¶
Index ¶
- type Cache
- func (c *Cache[T]) Clear()
- func (c *Cache[T]) Has(lookup string, keyParts ...any) bool
- func (c *Cache[T]) IgnoreErrors(ignore func(error) bool)
- func (c *Cache[T]) Invalidate(lookup string, keyParts ...any)
- func (c *Cache[T]) Load(lookup string, load func() (T, error), keyParts ...any) (T, error)
- func (c *Cache[T]) SetEvictionCallback(hook func(T))
- func (c *Cache[T]) SetInvalidateCallback(hook func(T))
- func (c *Cache[T]) Store(value T, store func() error) error
- func (c *Cache[T]) Trim(perc float64)
- type Lookup
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 ¶
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 ¶
Has checks the cache for a positive result under the given lookup and key parts.
func (*Cache[T]) IgnoreErrors ¶ added in v3.2.0
IgnoreErrors allows setting a function hook to determine which error types should / not be cached.
func (*Cache[T]) Invalidate ¶
Invalidate will invalidate any result from the cache found under given lookup and key parts.
func (*Cache[T]) Load ¶
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.
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.