mirror of
https://github.com/samuelncui/acp.git
synced 2026-09-20 22:44:15 +00:00
fix: harden copy pipeline and testing
This commit is contained in:
@@ -1,15 +1,22 @@
|
||||
package acp
|
||||
|
||||
import "sync"
|
||||
|
||||
type cacheEntry[V any] struct {
|
||||
once sync.Once
|
||||
value V
|
||||
}
|
||||
|
||||
func Cache[K comparable, V any](f func(in K) V) func(in K) V {
|
||||
cache := make(map[K]V, 0)
|
||||
var cache sync.Map
|
||||
return func(in K) V {
|
||||
cached, has := cache[in]
|
||||
if has {
|
||||
return cached
|
||||
cached, has := cache.Load(in)
|
||||
if !has {
|
||||
cached, _ = cache.LoadOrStore(in, new(cacheEntry[V]))
|
||||
}
|
||||
|
||||
out := f(in)
|
||||
cache[in] = out
|
||||
return out
|
||||
entry := cached.(*cacheEntry[V])
|
||||
entry.once.Do(func() { entry.value = f(in) })
|
||||
return entry.value
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user