readiness

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2021 License: MIT Imports: 3 Imported by: 0

README

Readiness

When the data we need to get is not locally, we always need to make sure that the data is ready.

Samples

Use readiness with default
import(
    "github.com/thecxx/readiness"
)

func main() {
    readiness.Register(
        "test_key",
        func(key string) (interface{}, error) {
            return "hello world", nil
        },
        2*time.Second,)

    value := readiness.Get("test_key")
}

Use readiness with options
import(
    "github.com/thecxx/readiness"
)

func main() {
    ready := readiness.New(
        readiness.WithPullFailedHandler(func(key string, err error) {
            // If there is an error when getting, we can know it
        }))
    ready.Register(
        "test_key",
        func(key string) (interface{}, error) {
            return "hello world", nil
        },
        2*time.Second,)

    value := ready.Get("test_key")
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Get

func Get(key string) interface{}

Get value with key.

func GetDefault

func GetDefault(key string, def interface{}) interface{}

Get value with key.

func Register

func Register(key string, pull PullHandlerFunc, expire time.Duration)

Register a new data source.

Types

type Option

type Option func(*Readiness)

func WithPullFailedHandler

func WithPullFailedHandler(handler func(string, error)) Option

With pull failed handler func.

type PullHandlerFunc

type PullHandlerFunc func(string) (interface{}, error)

type Readiness

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

func New

func New(opts ...Option) *Readiness

Get a new Readiness.

func (*Readiness) Get

func (r *Readiness) Get(key string) interface{}

Get value with key.

func (*Readiness) GetDefault

func (r *Readiness) GetDefault(key string, def interface{}) interface{}

Get value with key.

func (*Readiness) Register

func (r *Readiness) Register(key string, pull PullHandlerFunc, expire time.Duration)

Register a new data source.

Jump to

Keyboard shortcuts

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