memoize

package module
v0.0.0-...-9e5eb99 Latest Latest
Warning

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

Go to latest
Published: May 6, 2024 License: MIT Imports: 4 Imported by: 17

README

go-memoize

There wasn't a decent memoizer for Golang out there, so I lashed two nice libraries together and made one.

Dead-simple. Safe for concurrent use.

Reference Linter Build status

Project status

Complete. Latest commit timestamp might be old - that's okay.

Go-memoize has been in production for a few years, and has yet to burn the house down.

Usage

Cache expensive function calls in memory, with a configurable timeout and purge interval:

import (
	"time"

	"github.com/kofalt/go-memoize"
)

// Any expensive call that you wish to cache
expensive := func() (any, error) {
	time.Sleep(3 * time.Second)
	return "some data", nil
}

// Cache expensive calls in memory for 90 seconds, purging old entries every 10 minutes.
cache := memoize.NewMemoizer(90*time.Second, 10*time.Minute)

// This will call the expensive func
result, err, cached := cache.Memoize("key1", expensive)

// This will be cached
result, err, cached = cache.Memoize("key1", expensive)

// This uses a new cache key, so expensive is called again
result, err, cached = cache.Memoize("key2", expensive)

In the example above, result is:

  1. the return value from your function if cached is false, or
  2. a previously stored value if cached is true.

All the hard stuff is punted to go-cache and sync/singleflight, I just lashed them together.
Note that cache.Storage is exported, so you can use underlying features such as Flush or SaveFile.

Type safety

The default usage stores and returns an any type.
If you wants to store & retrieve a specific type, use Call instead:

import (
	"time"

	"github.com/kofalt/go-memoize"
)

// Same example as above, but this func returns a string!
expensive := func() (string, error) {
	time.Sleep(3 * time.Second)
	return "some data", nil
}

// Same as before
cache := memoize.NewMemoizer(90*time.Second, 10*time.Minute)

// This will call the expensive func, and return a string.
result, err, cached := memoize.Call(cache, "key1", expensive)

// This will be cached
result, err, cached = memoize.Call(cache, "key1", expensive)

// This uses a new cache key, so expensive is called again
result, err, cached = memoize.Call(cache, "key2", expensive)

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrMismatchedType = errors.New("data returned does not match expected type")

ErrMismatchedType if data returned from the cache does not match the expected type.

Functions

func Call

func Call[T any](m *Memoizer, key string, fn MemoizedFunction[T]) (T, error, bool)

Call executes and returns the results of the given function, unless there was a cached value of the same key. Only one execution is in-flight for a given key at a time. The boolean return value indicates whether v was previously stored.

Types

type MemoizedFunction

type MemoizedFunction[T any] func() (T, error)

MemoizedFunction the expensive function to be called.

type Memoizer

type Memoizer struct {

	// Storage exposes the underlying cache of memoized results to manipulate as desired - for example, to Flush().
	Storage *cache.Cache
	// contains filtered or unexported fields
}

Memoizer allows you to memoize function calls. Memoizer is safe for concurrent use by multiple goroutines.

func NewMemoizer

func NewMemoizer(defaultExpiration, cleanupInterval time.Duration) *Memoizer

NewMemoizer creates a new Memoizer with the configured expiry and cleanup policies. If desired, use cache.NoExpiration to cache values forever.

func (*Memoizer) Memoize

func (m *Memoizer) Memoize(key string, fn func() (interface{}, error)) (interface{}, error, bool)

Memoize executes and returns the results of the given function, unless there was a cached value of the same key. Only one execution is in-flight for a given key at a time. The boolean return value indicates whether v was previously stored.

Jump to

Keyboard shortcuts

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