Documentation
¶
Overview ¶
Package memoize provides a duplicate function call suppression and caching mechanism. It is similar to golang.org/x/sync/singleflight, but it caches the results.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Group ¶
type Group[K comparable, V any] struct { // contains filtered or unexported fields }
Group represents a class of work and forms a namespace in which units of work can be executed with duplicate suppression.
func (*Group[K, V]) Do ¶
func (g *Group[K, V]) Do(ctx context.Context, key K, fn func(ctx context.Context, key K) (val V, expiresAt time.Time, err error)) (V, time.Time, error)
Do executes and memoizes the results of the given function, making sure that only one execution is in-flight for a given key at a time. If a duplicate comes in, the duplicate caller waits for the original to complete and receives the same results. The memoized results are available until expiredAt.
Example ¶
package main
import (
"context"
"fmt"
"time"
"github.com/shogo82148/memoize"
)
func main() {
var g memoize.Group[string, string]
fn := func(ctx context.Context, key string) (v string, expiredAt time.Time, err error) {
fmt.Println("fn is just called once!")
// it takes 100 ms to execute this function.
time.Sleep(100 * time.Millisecond)
// the cache is available in 1 second.
expiredAt = time.Now().Add(time.Second)
return
}
// duplicate function calls of Do just call fn once.
go g.Do(context.Background(), "key", fn)
go g.Do(context.Background(), "key", fn)
g.Do(context.Background(), "key", fn)
// this call returns the cached result.
time.Sleep(900 * time.Millisecond)
g.Do(context.Background(), "key", fn)
}
Output: fn is just called once!
func (*Group[K, V]) DoChan ¶ added in v0.0.2
func (g *Group[K, V]) DoChan(ctx context.Context, key K, fn func(ctx context.Context, key K) (val V, expiresAt time.Time, err error)) <-chan Result[V]
DoChan is like Do but returns a channel that will receive the results when they are ready.
The returned channel will not be closed.