memoize

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Sep 13, 2023 License: MIT Imports: 6 Imported by: 0

README

gocache-memoize

Memoization implemented using eko/gocache.

Installation

To begin working with the latest version of gocache-memoize, you can import the library in your project:

go get github.com/ketch-com/gocache-memoize

Usage

import (
	"github.com/eko/gocache/lib/v4/cache"
	"github.com/ketch-com/gocache-memoize"
)

cacheManager := cache.New[[]byte](...)

m := memoize.NewMemoizer(cacheManager)
value, err := m.Memoize(ctx, "key1", func(ctx context.Context) (any, error) {
	return "test", nil
})

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Memoizer

type Memoizer[T any] struct {
	*cache.Cache[T]
	// contains filtered or unexported fields
}

Memoizer provides a mechanism to memoize results of functions

func NewMemoizer

func NewMemoizer[T any](cache *cache.Cache[T]) *Memoizer[T]

NewMemoizer returns a new Memoizer

func (*Memoizer[T]) Clear added in v0.0.3

func (m *Memoizer[T]) Clear(ctx context.Context) error

func (*Memoizer[T]) Delete added in v0.0.3

func (m *Memoizer[T]) Delete(ctx context.Context, key any) error

func (*Memoizer[T]) Memoize

func (m *Memoizer[T]) Memoize(ctx context.Context, key any, fn func(context.Context) (T, error)) (T, error)

Memoize returns the last value of the function fn when called with the given key. If not available in the cache, then the function is called and its result is cached.

Jump to

Keyboard shortcuts

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