loader

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2022 License: MIT Imports: 6 Imported by: 1

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)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CacheDriver added in v0.3.0

type CacheDriver interface {
	Add(key interface{}, value interface{})
	Get(key interface{}) (interface{}, bool)
}

CacheDriver stores the items you can use ARCCache or TwoQueueCache from github.com/hashicorp/golang-lru

func InMemoryCache added in v0.1.1

func InMemoryCache() CacheDriver

type ContextFactory added in v0.3.0

type ContextFactory func() context.Context

ContextFactory creates context to be used by LoadFunc

type Fetcher added in v0.3.0

type Fetcher[Key comparable, Value any] func(ctx context.Context, key Key) (Value, error)

Fetcher loads the value based on key

type InMemoryKeyLocker added in v0.3.0

type InMemoryKeyLocker[Key comparable] struct {
	// contains filtered or unexported fields
}

func (*InMemoryKeyLocker[Key]) Lock added in v0.3.0

func (l *InMemoryKeyLocker[Key]) Lock(key Key) func()

Lock implements KeyLocker

type KeyLocker added in v0.3.0

type KeyLocker[Key comparable] interface {
	Lock(key Key) (unlock func())
}

type Loader

type Loader[Key comparable, Value any] struct {
	// contains filtered or unexported fields
}

Loader manage items in cache and fetch them if not exist

func New

func New[Key comparable, Value any](fn Fetcher[Key, Value], ttl time.Duration, options ...Option) *Loader[Key, Value]

New creates new Loader

func NewLRU

func NewLRU[Key comparable, Value any](fn Fetcher[Key, Value], ttl time.Duration, size int, options ...Option) *Loader[Key, Value]

NewLRU creates Loader with lru based cache

func (*Loader[Key, Value]) Load added in v0.3.0

func (l *Loader[Key, Value]) Load(key Key) (Value, error)

Load the item. If it doesn't exist on cache, Loader will call LoadFunc once even when other go routine access the same key. If the item is expired, it will return old value while loading new one.

type Option added in v0.3.0

type Option func(cfg *config)

func WithContextFactory added in v0.3.0

func WithContextFactory(cf ContextFactory) Option

func WithDriver added in v0.3.0

func WithDriver(driver CacheDriver) Option

func WithErrorTTL added in v0.3.0

func WithErrorTTL(ttl time.Duration) Option

Jump to

Keyboard shortcuts

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