mirror of
https://github.com/writeas/web-core
synced 2018-07-20 11:55:21 +00:00
Add memoization module
This commit is contained in:
parent
dfbe258744
commit
39a0df98f6
56
memo/memo.go
Normal file
56
memo/memo.go
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
package memo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
type (
|
||||||
|
Memo struct {
|
||||||
|
f Func
|
||||||
|
mu sync.Mutex //guards cache
|
||||||
|
cache *entry
|
||||||
|
}
|
||||||
|
|
||||||
|
Func func() (interface{}, error)
|
||||||
|
)
|
||||||
|
|
||||||
|
type (
|
||||||
|
entry struct {
|
||||||
|
res result
|
||||||
|
ready chan struct{} // close when res is read
|
||||||
|
}
|
||||||
|
|
||||||
|
result struct {
|
||||||
|
value interface{}
|
||||||
|
err error
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
func New(f Func) *Memo {
|
||||||
|
return &Memo{
|
||||||
|
f: f,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (memo *Memo) Invalidate() {
|
||||||
|
memo.mu.Lock()
|
||||||
|
memo.cache = nil
|
||||||
|
memo.mu.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (memo *Memo) Get() (value interface{}, err error) {
|
||||||
|
memo.mu.Lock()
|
||||||
|
if memo.cache == nil {
|
||||||
|
memo.cache = &entry{ready: make(chan struct{})}
|
||||||
|
memo.mu.Unlock()
|
||||||
|
|
||||||
|
memo.cache.res.value, memo.cache.res.err = memo.f()
|
||||||
|
|
||||||
|
close(memo.cache.ready)
|
||||||
|
} else {
|
||||||
|
memo.mu.Unlock()
|
||||||
|
|
||||||
|
<-memo.cache.ready
|
||||||
|
}
|
||||||
|
return memo.cache.res.value, memo.cache.res.err
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user