/sync2-go

Additional functionality augmenting package sync.

Primary LanguageGo

Package sync2

Package sync2 provides Cached, a concurrency-safe way to lazily-load and then cache a long-lived value.

On success:

var cached sync2.Cached
cached.Get(fn) // fn is called, and its return values are returend
cached.Get(fn) // fn is not called, and the cached values are returned

If an error is returned:

fn := func() (interface{}, error) {
	return nil, errors.New("init failed")
}
var cached sync2.Cached
val, err := cached.Get(fn) // fn is called
val, err = cached.Get(fn)  // fn is _not_ called, but the cached error is returned

// ... wait 10 seconds

val, err = cached.Get(fn)  // fn is called again

When reset is called:

var cached sync2.Cached
val, err := cached.Get(fn) // fn is called
cached.Reset()
val, err = cached.Get(fn) // fn is not called

// ... wait 10 seconds

val, err = cached.Get(fn) // fn is called again