memoize

package module
v0.0.0-...-b0319ca Latest Latest
Warning

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

Go to latest
Published: Sep 25, 2024 License: BSD-3-Clause Imports: 10 Imported by: 0

README

memoize

WARNING: This project is in early development and is not yet ready for use.

Memoize is a library for memorizing function return values based on function arguments. The main use case is to avoid making duplicate expensive IO operations or computations multiple times.

The most basic usage looks like this.

package main

import (
	"fmt"

	"github.com/alx99/memoize"
)

func expensiveFunc(val int) int {
	fmt.Println("Expensive function executed")
	return val
}

func main() {
	memoized := memoize.Auto(expensiveFunc)
	memoized(1) // expensiveFunc is called
	memoized(2) // expensiveFunc is called
	memoized(1) // expensiveFunc is not called

	manualMemoized := memoize.Manual[func(int) int, string](expensiveFunc)
	manualMemoized("key1")(1) // expensiveFunc is called
	manualMemoized("key2")(2) // expensiveFunc is called
	manualMemoized("key1")(2) // expensiveFunc is not called and 1 is returned
}

Please read the documentation for more information.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Auto

func Auto[T any](f T, opts ...MemoizerOption) T

Auto returns a memoized version of the function f By default it uses MapCache as the cache and Fn64aKeyer as the KeyerFunc.

Example
i := 0
expensiveFunc := func(x int) {
	i += x
}

memoized := Auto(expensiveFunc) // memoize.Auto

memoized(100)
memoized(1)
memoized(1)

fmt.Println(i)
Output:

101

func Manual

func Manual[T any, V comparable](f T, opts ...MemoizerOption) func(V) T

Manual returns a function F(V) T that returns the memoized function f. The key needs to be explicitly be passed to F(V).

Example
i := 0
expensiveFunc := func(x int) {
	i += x
}

memoized := Manual[func(int), string](expensiveFunc) // memoize.Manual

memoized("key1")(100)
memoized("key2")(1)
memoized("key2")(1)

fmt.Println(i)
Output:

101

Types

type Cache

type Cache interface {
	// Get returns the cached result for the given args
	Get(args []reflect.Value) ([]reflect.Value, bool)
	// Set associates the given args with the given res
	Set(args []reflect.Value, res []reflect.Value)
	// Clear clears the cache
	Clear()
	// Len returns the number of cached argument calls
	Len() int
}

Cache is the interface needed to be satisfied by Cache implementations. Implementations needs be safe for concurrent use.

type KeyerFunc

type KeyerFunc[T comparable] func(args []reflect.Value) T

func Fn64aKeyer

func Fn64aKeyer() KeyerFunc[uint64]

type MapCache

type MapCache[T comparable] struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

MapCache is a cache that uses a map to store the results

func NewMapCache

func NewMapCache[T comparable](keyer KeyerFunc[T], opts ...MapCacheOption) *MapCache[T]

func (*MapCache[T]) Clear

func (d *MapCache[T]) Clear()

func (*MapCache[T]) Get

func (d *MapCache[T]) Get(args []reflect.Value) ([]reflect.Value, bool)

func (*MapCache[T]) Len

func (d *MapCache[T]) Len() int

func (*MapCache[T]) Set

func (d *MapCache[T]) Set(args []reflect.Value, res []reflect.Value)

type MapCacheOption

type MapCacheOption func(*MapCache[any])

MapCacheOption is an option that can be set to a MapCache

func WithCleanDur

func WithCleanDur(ctx context.Context, dur time.Duration) MapCacheOption

WithCleanDur sets the duration between when the cache is cleared

type MemoizerOption

type MemoizerOption func(*memoizer[any])

MemoizerOption is a function that sets an option on the memoizer

func WithCache

func WithCache(cache Cache) MemoizerOption

WithCache sets the Cache to use

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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