Documentation ¶
Index ¶
- type Cache
- func (c *Cache[Value]) Clear()
- func (c *Cache[Value]) Has(lookup string, keyParts ...any) bool
- func (c *Cache[Value]) IgnoreErrors(ignore func(error) bool)
- func (c *Cache[Value]) Invalidate(lookup string, keyParts ...any)
- func (c *Cache[Value]) Load(lookup string, load func() (Value, error), keyParts ...any) (Value, error)
- func (c *Cache[Value]) SetEvictionCallback(hook func(Value))
- func (c *Cache[Value]) SetInvalidateCallback(hook func(Value))
- func (c *Cache[Value]) SetTTL(ttl time.Duration, update bool)
- func (c *Cache[Value]) Start(freq time.Duration) bool
- func (c *Cache[Value]) Stop() bool
- func (c *Cache[Value]) Store(value Value, store func() error) error
- type Lookup
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 ¶
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 ¶
Has checks the cache for a positive result under the given lookup and key parts.
func (*Cache[Value]) IgnoreErrors ¶ added in v3.2.0
IgnoreErrors allows setting a function hook to determine which error types should / not be cached.
func (*Cache[Value]) Invalidate ¶
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 ¶
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 ¶
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.
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 }
Lookup represents a struct object lookup method in the cache.