Categorygithub.com/abihf/cache-loader
modulepackage
0.3.0
Repository: https://github.com/abihf/cache-loader.git
Documentation: pkg.go.dev

# README

cache-loader

Release go.dev reference Go codecov

Golang Cache Loader

Feature

  • Thread safe
  • Fetch once even when concurrent process request same key.
  • stale-while-revalidate when item is expired

Example

func main() {
  itemLoader := loader.NewLRU(fetchItem, 5 * time.Minute, 1000)
  item, err := loader.Get("key")
  // use item
}

func fetchItem(key interface{}) (interface{}, error) {
  res, err := http.Get("https://example.com/item/" + key)
  if err != nil {
    return nil, err
  }
  return processResponse(res)
}

# Functions

No description provided by the author
New creates new Loader.
NewLRU creates Loader with lru based cache.
No description provided by the author
No description provided by the author
No description provided by the author

# Structs

No description provided by the author
Loader manage items in cache and fetch them if not exist.

# Interfaces

CacheDriver stores the items you can use ARCCache or TwoQueueCache from github.com/hashicorp/golang-lru.
No description provided by the author

# Type aliases

ContextFactory creates context to be used by LoadFunc.
Fetcher loads the value based on key.
No description provided by the author