memoize

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 14, 2024 License: MIT Imports: 8 Imported by: 2

README

memoize

test Go Reference Coverage Status

Package memoize provides a duplicate function call suppression and caching mechanism. It is similar to x/sync/singleflight, but it caches the results.

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

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.

func (*Group[K, V]) Forget added in v0.0.2

func (g *Group[K, V]) Forget(key K)

Forget tells the singleflight to forget about a key. Future calls to Do for this key will call the function rather than waiting for an earlier call to complete.

func (*Group[K, V]) GC

func (g *Group[K, V]) GC()

GC deletes the expired items from the cache.

type Result added in v0.0.2

type Result[V any] struct {
	Val       V
	ExpiresAt time.Time
	Err       error
}

Jump to

Keyboard shortcuts

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