mirror of
https://github.com/samuelncui/yatm.git
synced 2026-01-09 22:47:28 +00:00
18 lines
265 B
Go
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
|
|
}
|
|
}
|