Files
yatm/tools/cache.go
2023-09-22 23:14:58 +08:00

18 lines
265 B
Go

package tools
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.(V)
}
out := f(in)
cache.Store(in, out)
return out
}
}