gofresher

package module
v0.0.0-...-8214a20 Latest Latest
Warning

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

Go to latest
Published: Jan 26, 2024 License: MIT Imports: 3 Imported by: 0

README

Gofresher

Scheduled and just-in-time state refresher, Gofresher allows to manage a central state that is refreshed both periodically and opportunistically. In all cases, refresh actions are deduplicated.

// Create a state holder where a value is does not need refreshing for 50 minutes
tokenHolder := gofresher.NewGofresher[string](50*time.Minute, func(state *int) (*string, error) {
  token, err := fetchHourLongToken()

  return token, err
})

// Start a background task to check if the token needs refreshing every minute
tokenHolder.Start(time.Minute)

// Retrieve the current state without refreshing
token, err, refreshed := tokenHolder.State(false)

// Retrieve the current state, refreshing if needed
token, err, refreshed := tokenHolder.State(true)

// Force refreshing the current state
token, err := tokenHolder.ForceRefresh()

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Gofresher

type Gofresher[S any] struct {
	// contains filtered or unexported fields
}

func NewGofresher

func NewGofresher[S any](cache time.Duration, refreshFunc RefreshFunc[S]) *Gofresher[S]

func (*Gofresher[S]) ForceRefresh

func (gr *Gofresher[S]) ForceRefresh() (*S, error)

func (*Gofresher[S]) Start

func (gr *Gofresher[S]) Start(tick time.Duration)

func (*Gofresher[S]) State

func (gr *Gofresher[S]) State(refreshable bool) (*S, error, bool)

type RefreshFunc

type RefreshFunc[S any] func(*S) (*S, error)

Jump to

Keyboard shortcuts

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