Skip to content
/ memo Public

Teeny-weeny cached variable library with auto refresh and anti stampede.

License

Notifications You must be signed in to change notification settings

ganglio/memo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Memo

GoDoc Go Report Card

Teeny-weeny cached variable library with auto refresh and anti stampede.

Usage

v := 0
counter := M[int](func() int{
  v=v+1
  return v
}).Memo(time.Second)

for {
  fmt.Printf("Counter %d", counter())
}

If you function can return an error, use GX like following:

v := 0
counter := MX[int](func() (int, error){
  if (v<0) {
    return v, errors.New("Cannot start with a negative counter, for some reason...")
  }
  v := v+1
  return v, nil
}).Memo(time.Second)

for {
  fmt.Printf("Counter %d", counter())
}

In this case it works like this:

  • If the generator returns an error, the Memo method will return the error upon first cache warmup.
  • If not it will return the wrapped function.
  • After the first initialisation, if the generator returns an error, it will be returned without updating the cached value.
    • This means that the errors must be handled inside the generator.

About

Teeny-weeny cached variable library with auto refresh and anti stampede.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages