fix: restore result report

This commit is contained in:
Samuel N Cui
2023-09-22 23:14:58 +08:00
parent d206218634
commit eeb0f4f7bf
4 changed files with 31 additions and 17 deletions

View File

@@ -1,15 +1,17 @@
package tools
func Cache[i comparable, o any](f func(in i) o) func(in i) o {
cache := make(map[i]o, 0)
return func(in i) o {
cached, has := cache[in]
import "sync"
func Cache[K comparable, V any](f func(in K) V) func(in K) V {
cache := new(sync.Map)
return func(in K) V {
cached, has := cache.Load(in)
if has {
return cached
return cached.(V)
}
out := f(in)
cache[in] = out
cache.Store(in, out)
return out
}
}