once

package
v0.0.0-...-6e3bad2 Latest Latest
Warning

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

Go to latest
Published: Dec 21, 2024 License: Apache-2.0 Imports: 4 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 struct {
	// contains filtered or unexported fields
}

func New

func New(expirationTime time.Duration, name string) (*Cache, error)

New creates a cache where items are evicted after being present for `expirationTime`. `name` is used to name the gauge used for metrics reporting.

func (*Cache) GetOrRetrieve

func (c *Cache) GetOrRetrieve(ctx context.Context, key string, retriever Retriever) (interface{}, error)

GetOrRetrieve will return the cached value and if it isn't present will call `Retriever`. It is guaranteed there will only on one concurrent call to `Retriever` for each key, other accesses to the key will be blocked until `Retriever.Call` returns. If `Retriever.Call` fails the error will not be cached and subsequent calls will call the `Retriever` again.

type Retriever

type Retriever interface {
	// Call method called when a cache miss happens which will return the actual value that needs to be cached
	Call(ctx context.Context, key string) (interface{}, error)
}

type RetrieverFunc

type RetrieverFunc func(context.Context, string) (interface{}, error)

func (RetrieverFunc) Call

func (f RetrieverFunc) Call(ctx context.Context, key string) (interface{}, error)

Jump to

Keyboard shortcuts

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