cache

package
v0.0.0-...-f9509b0 Latest Latest
Warning

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

Go to latest
Published: Jun 21, 2024 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithCacheUpdateHandler

func WithCacheUpdateHandler[T any](updateHandler CacheUpdateHandler[T]) option

func WithErrorHandler

func WithErrorHandler(errorHandler ErrorHandler) option

func WithLogger

func WithLogger(log *zap.Logger) option

func WithMetricsSink

func WithMetricsSink(sink metrics.MetricSink) option

func WithRetryAfterExpirarionDelay

func WithRetryAfterExpirarionDelay(delay time.Duration) option

If a value is not renewed prior to its expiration, the maintainer uses a linear retry with a default delay of 10s. WithRetryAfterExpirarionDelay sets this delay to another value if desired by the user. Decreasing the delay does speedup recovery once the provider is available, but also increases the impact of the provider on recovery.

func WithRetryOptions

func WithRetryOptions(retryOpts []retry.Option) option

Types

type CacheUpdateHandler

type CacheUpdateHandler[T any] func(T)

CacheUpdateHandler is called every time the cached value is updated if requested as an option The value provided is the ExpiringValue construct which is itself containing the value within the cache The handler must not modify the value provided

type ErrorHandler

type ErrorHandler func(error)

ErrorHandler is called every time an error is returned when attempting to update the cached value

type ExpiringValue

type ExpiringValue[T any] struct {
	Value     T
	ExpiresAt time.Time
}

type Fetcher

type Fetcher[T any] interface {
	// Stringer interface for returning a unique identifier
	fmt.Stringer

	// Fetch returns a valid ExpiringValue
	Fetch(ctx context.Context) (*ExpiringValue[T], error)
}

type Maintainer

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

Maintainer is a Refresh-Ahead Cache used to ensure that a ExpiringValue is kept valid for a given RefreshAtFunc. Any call to execute must be done under the syncLock to ensure there is no concurrent calls to the provider

func NewMaintainer

func NewMaintainer[T any](fetcher Fetcher[T], refreshAtFunc RefreshAtFunc, options ...option) *Maintainer[T]

func (*Maintainer[T]) Close

func (m *Maintainer[T]) Close()

func (*Maintainer[T]) Get

func (m *Maintainer[T]) Get(ctx context.Context) (T, error)

Get returns the ExpiringValue returned by Fetcher until the ExpiringValue expires.

func (*Maintainer[T]) IsClosed

func (m *Maintainer[T]) IsClosed() bool

type RefreshAtFunc

type RefreshAtFunc func(notBefore time.Time, notAfter time.Time) time.Time

RefreshAtFunc returns the time.Time to re-fetch the ExpiringValue. This returned time.Time should never be in the past.

func NewPercentageRemainingRefreshAt

func NewPercentageRemainingRefreshAt(renewAfterPercentage float64, jitterPercentage float64) RefreshAtFunc

NewPercentageRemainingRefreshAt is a basic RefreshAtFunc for calculating the time.Time at which to fetch a ExpiringValue based on the remaining percentage of the lifetime of a ExpiringValue.

renewAfterPercentage is the percentage of time remaining at which point a ExpiringValue should be re-fetched. jitterPercentage is the maximum percentage of (notAfter.Sub(notBefore) * renewAfterPercentage) to use for calculating a jitter value.

i.e. if notBefore and notAfter are 60s apart, renewAfterPercentage is 0.33, and jitter percentage is 10%, the non jittered time will be 60s * 0.33, or 20s from now, with an added jitter of 0-2 (20s * 10%) seconds, for a final return value evenly spaced between 20 and 22 seconds from now.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳