cache

package
v0.0.0-...-4fd39d7 Latest Latest
Warning

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

Go to latest
Published: May 30, 2025 License: MIT Imports: 3 Imported by: 0

Documentation

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

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]) Stop

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

Stop will shutdown the background cleanup for 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