go_memoize
package provides a set of functions to memoize the results of computations, allowing for efficient caching and retrieval of results based on input parameters. This can significantly improve performance for expensive or frequently called functions.
- Memoizes functions with TTL, supporting 0 to 7 comparable parameters. List of Memoize Functions
- High performance, zero allocation, and zero dependencies.
- Utilizes the FNV-1a hash algorithm for caching.
- Thread-safe and concurrent-safe.
To install the package, use go get
:
go get github.com/AhmedGoudaa/go_memoize
The Memoize
function can be used to memoize a function with no parameters:
computeFn := func() int {
// Expensive computation
return 42
}
memoizedFn := Memoize(computeFn, 10*time.Second)
result := memoizedFn()
The package provides functions to memoize functions with up to 7 parameters. Here are some examples:
computeFn := func(a int) int {
// Expensive computation
return a * 2
}
memoizedFn := Memoize1(computeFn, 10*time.Second)
result := memoizedFn(5)
computeFn := func(a int, b string) string {
// Expensive computation
return fmt.Sprintf("%d-%s", a, b)
}
memoizedFn := Memoize2(computeFn, 10*time.Second)
result := memoizedFn(5, "example")
computeFn := func(a int, b string, c float64) string {
// Expensive computation
return fmt.Sprintf("%d-%s-%f", a, b, c)
}
memoizedFn := Memoize3(computeFn, 10*time.Second)
result := memoizedFn(5, "example", 3.14)
The Cache
struct is used internally to manage the cached entries. It supports setting, getting, and deleting entries, as well as computing new values if they are not already cached or have expired.
Here is a complete example of using the memoize
package:
package main
import (
"fmt"
"time"
m "github.com/AhmedGoudaa/go_memoize"
)
func main() {
computeFn := func(a int, b string) string {
// Simulate an expensive computation
time.Sleep(2 * time.Second)
return fmt.Sprintf("%d-%s", a, b)
}
memoizedFn := m.Memoize2(computeFn, 10*time.Second)
// First call will compute the result
result := memoizedFn(5, "example")
fmt.Println(result) // Output: 5-example
// Subsequent calls within 10 seconds will use the cached result
result = memoizedFn(5, "example")
fmt.Println(result) // Output: 5-example
}
Function |
Description |
Example |
---|---|---|
Memoize |
Memoizes a function with no params |
|
Memoize1 |
Memoizes a function with 1 param |
|
Memoize2 |
Memoizes a function with 2 params |
|
Memoize3 |
Memoizes a function with 3 params |
|
Memoize4 |
Memoizes a function with 4 params |
|
Memoize5 |
Memoizes a function with 5 params |
|
Memoize6 |
Memoizes a function with 6 params |
|
Memoize7 |
Memoizes a function with 7 params |
|
Device "Apple M2 Pro"
goos: darwin
goarch: arm64
BenchmarkDo0Mem-10 | 811289566 | 14.77 ns/op | 0 B/op | 0 allocs/op
BenchmarkDo1Mem-10 | 676579908 | 18.26 ns/op | 0 B/op | 0 allocs/op
BenchmarkDo2Mem-10 | 578134332 | 20.99 ns/op | 0 B/op | 0 allocs/op
BenchmarkDo3Mem-10 | 533455237 | 22.67 ns/op | 0 B/op | 0 allocs/op
BenchmarkDo4Mem-10 | 487471639 | 24.73 ns/op | 0 B/op | 0 allocs/op
This project is licensed under the Apache License. See the LICENSE
file for details.