memoize

package
v0.1.37 Latest Latest
Warning

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

Go to latest
Published: Jun 25, 2025 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Memoize

func Memoize[F any](f F, options ...Option) F

func MemoizeWithInvalidate added in v0.1.34

func MemoizeWithInvalidate[F any](f F, options ...Option) (F, func())

MemoizeWithInvalidate wraps a function with a caching layer that stores and reuses computed results. It returns two values:

  • A memoized version of the input function with identical signature
  • An invalidation function that clears the cache when called

The memoized function caches results based on the string representation of input arguments. Cache entries expire based on time and size limits set via options.

Options:

  • WithMaxSize: Sets maximum number of cached results (default 100)
  • WithExpiration: Sets how long cached results remain valid (default 1 hour)
  • WithIgnoreParams: Specifies argument indices to exclude from cache key
  • WithMemoizeErrors: Controls whether error results are cached (default false)
  • WithMetrics: Provides a collector for cache performance metrics

Example:

memoizedFn, invalidate := Memoize(expensiveFunc, WithMaxSize(1000))
result := memoizedFn(arg) // Result is computed and cached
invalidate() // Clears the cache

Types

type MemoMetrics added in v0.1.14

type MemoMetrics struct {
	Hits       atomic.Int64
	Misses     atomic.Int64
	Evictions  atomic.Int64
	TotalItems int
}

type MetricsCollector added in v0.1.17

type MetricsCollector interface {
	Setup(function interface{})
	Collect(metrics *MemoMetrics)
}

type Option added in v0.1.14

type Option func(*memoizeOptions)

func WithExpiration

func WithExpiration(d time.Duration) Option

func WithIgnoreParams added in v0.1.23

func WithIgnoreParams(indices []int) Option

func WithMaxSize

func WithMaxSize(size int) Option

func WithMemoizeErrors added in v0.1.33

func WithMemoizeErrors(memoize bool) Option

func WithMetrics added in v0.1.17

func WithMetrics(collector MetricsCollector) Option

type PrometheusMetricsCollector added in v0.1.17

type PrometheusMetricsCollector struct {
	// contains filtered or unexported fields
}

func NewPrometheusMetricsCollector added in v0.1.17

func NewPrometheusMetricsCollector(labels ...string) *PrometheusMetricsCollector

func (*PrometheusMetricsCollector) Collect added in v0.1.17

func (p *PrometheusMetricsCollector) Collect(metrics *MemoMetrics)

func (*PrometheusMetricsCollector) Setup added in v0.1.17

func (p *PrometheusMetricsCollector) Setup(function interface{})

Jump to

Keyboard shortcuts

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