lazyref

package
v0.0.0-...-d4adf30 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2020 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Examples

Constants

View Source
const (
	// InitOnFirstAccess specifies that the reference should be initialized the first time it is accessed
	InitOnFirstAccess time.Duration = time.Duration(-1)

	// InitImmediately specifies that the reference should be initialized immediately after it is created
	InitImmediately time.Duration = time.Duration(0)
)

Variables

This section is empty.

Functions

func WithAbsoluteExpiration

func WithAbsoluteExpiration(value time.Duration) options.Opt

WithAbsoluteExpiration sets the expiration time for the reference. It will expire after this time period, regardless of whether or not it has been recently accessed.

func WithExpirationProvider

func WithExpirationProvider(expirationProvider ExpirationProvider, expiryType ExpirationType) options.Opt

WithExpirationProvider sets the expiration provider, which determines the expiration time of the reference

func WithFinalizer

func WithFinalizer(finalizer Finalizer) options.Opt

WithFinalizer sets a finalizer function that is called when the reference is closed or if it expires

func WithIdleExpiration

func WithIdleExpiration(value time.Duration) options.Opt

WithIdleExpiration sets the idle-time expiration for the reference. The reference is expired after not being accessed for the given duration.

func WithRefreshInterval

func WithRefreshInterval(initialInit, refreshPeriod time.Duration) options.Opt

WithRefreshInterval specifies that the reference should be proactively refreshed. Argument, initialInit, if greater than or equal to 0, indicates that the reference should be initialized after this duration. If less than 0, the reference will be initialized on first access. Argument, refreshPeriod, is the period at which the reference will be refreshed. Note that the Finalizer will not be invoked each time the value is refreshed.

Types

type ExpirationProvider

type ExpirationProvider func() time.Duration

ExpirationProvider is a function that returns the expiration time of a reference

func NewGraduatingExpirationProvider

func NewGraduatingExpirationProvider(initialExpiry, increments, maxExpiry time.Duration) ExpirationProvider

NewGraduatingExpirationProvider returns an expiration provider that has an initial expiration and then expires in graduated increments with a maximum expiration time.

func NewSimpleExpirationProvider

func NewSimpleExpirationProvider(expiry time.Duration) ExpirationProvider

NewSimpleExpirationProvider returns an expiration provider that sets the given expiration period

type ExpirationType

type ExpirationType uint

ExpirationType indicates how to handle expiration of the reference

const (
	// LastAccessed specifies that the expiration time is calculated
	// from the last access time
	LastAccessed ExpirationType = iota

	// LastInitialized specifies that the expiration time is calculated
	// from the time the reference was initialized
	LastInitialized

	// Refreshing indicates that the reference should be periodically refreshed
	Refreshing
)

type Finalizer

type Finalizer func(value interface{})

Finalizer is a function that is called when the reference is closed

type Initializer

type Initializer func() (interface{}, error)

Initializer is a function that initializes the value

type InitializerWithData

type InitializerWithData func(data interface{}) (interface{}, error)

InitializerWithData is a function that initializes the value using the optional data.

type Reference

type Reference struct {
	// contains filtered or unexported fields
}

Reference holds a value that is initialized on first access using the provided Initializer function. The Reference has an optional expiring feature wherin the value is reset after the provided period of time. A subsequent call to Get or MustGet causes the Initializer function to be invoked again. The Reference also has a proactive refresh capability, in which the Initializer function is periodically called out of band (out of band means that the caller of Get or MustGet does not need to wait for the initializer function to complete: the old value will be used until the new value has finished initializing). An optional Finalizer function may be provided to be invoked whenever the Reference is closed (via a call to Close) or if it expires. (Note: The Finalizer function is not called every time the value is refreshed with the periodic refresh feature.)

Example
ref := New(func() (interface{}, error) {
	return "Value1", nil
})
fmt.Println(ref.MustGet())
Output:

Value1
Example (Expiring)
sequence := 0
ref := New(
	func() (interface{}, error) {
		sequence++
		return fmt.Sprintf("Data_%d", sequence), nil
	},
	WithIdleExpiration(200*time.Millisecond),
)

for i := 0; i < 5; i++ {
	fmt.Println(ref.MustGet())
	time.Sleep(100 * time.Millisecond)
}
Output:

Example (Refreshing)

This example demonstrates a refreshing reference. The reference is initialized immediately after creation and every 2 seconds thereafter.

sequence := 0
ref := New(
	func() (interface{}, error) {
		sequence++
		return fmt.Sprintf("Data_%d", sequence), nil
	},
	WithRefreshInterval(InitImmediately, 200*time.Millisecond),
)

for i := 0; i < 5; i++ {
	fmt.Println(ref.MustGet())
	time.Sleep(300 * time.Millisecond)
}
Output:

func New

func New(initializer Initializer, opts ...options.Opt) *Reference

New creates a new reference

func NewWithData

func NewWithData(initializer InitializerWithData, opts ...options.Opt) *Reference

NewWithData creates a new reference where data is passed from the Get function to the initializer. This is useful for refreshing the reference with dynamic data.

func (*Reference) Close

func (r *Reference) Close()

Close ensures that the finalizer (if provided) is called. Close should be called for expiring references and rerences that specify finalizers.

func (*Reference) Get

func (r *Reference) Get(data ...interface{}) (interface{}, error)

Get returns the value, or an error if the initialiser returned an error.

func (*Reference) IsClosed

func (r *Reference) IsClosed() bool

IsClosed returns true if the referenced has been closed

func (*Reference) MustGet

func (r *Reference) MustGet() interface{}

MustGet returns the value. If an error is returned during initialization of the value then this function will panic.

func (*Reference) SetAbsoluteExpiration

func (p *Reference) SetAbsoluteExpiration(expiration time.Duration)

func (*Reference) SetExpirationProvider

func (p *Reference) SetExpirationProvider(expirationProvider ExpirationProvider, expiryType ExpirationType)

func (*Reference) SetFinalizer

func (p *Reference) SetFinalizer(value Finalizer)

func (*Reference) SetIdleExpiration

func (p *Reference) SetIdleExpiration(expiration time.Duration)

func (*Reference) SetRefreshInterval

func (p *Reference) SetRefreshInterval(initialInit, refreshPeriod time.Duration)

Jump to

Keyboard shortcuts

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