cache

package
v1.10.0 Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2022 License: Apache-2.0 Imports: 3 Imported by: 6

Documentation

Overview

Package cache implements an inmemory cache for any object.

Although exported, this package is non intended for general consumption. It is a shared dependency between multiple exposure notifications projects. We cannot guarantee that there won't be breaking changes in the future.

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidDuration = errors.New("expireAfter duration cannot be negative")

Functions

This section is empty.

Types

type Cache

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

func New

func New[T any](expireAfter time.Duration) (*Cache[T], error)

New creates a new in memory cache.

func (*Cache[T]) Clear added in v0.26.0

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

Clear removes all items from the cache, regardless of their expiration.

func (*Cache[T]) Lookup

func (c *Cache[T]) Lookup(name string) (T, bool)

Lookup checks the cache for a non-expired object by the supplied key name. The bool return informs the caller if there was a cache hit or not. A return of nil, true means that nil is in the cache. Where nil, false indicates a cache miss or that the value is expired and should be refreshed.

func (*Cache[T]) Set

func (c *Cache[T]) Set(name string, object T) error

Set saves the current value of an object in the cache, with the supplied durintion until the object expires.

func (*Cache[T]) Size

func (c *Cache[T]) Size() int

Size returns the number of items in the cache.

func (*Cache[T]) WriteThruLookup

func (c *Cache[T]) WriteThruLookup(name string, primaryLookup Func[T]) (T, error)

WriteThruLookup checks the cache for the value associated with name, and if not found or expired, invokes the provided primaryLookup function to local the value.

type Func

type Func[T any] func() (T, error)

Jump to

Keyboard shortcuts

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