Page MenuHomeMusing Studio

No OneTemporary

diff --git a/memo/memo.go b/memo/memo.go
index 8a88ccd..f603604 100644
--- a/memo/memo.go
+++ b/memo/memo.go
@@ -1,79 +1,81 @@
package memo
import (
"sync"
"time"
)
type (
Memo struct {
f Func
mu sync.Mutex //guards cache
cache *entry
cacheDur time.Duration
lastCache time.Time
}
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, cd time.Duration) *Memo {
return &Memo{
f: f,
cacheDur: cd,
}
}
// Invalidate resets the cache to nil if the memo's given cache duration has
// elapsed, and returns true if the cache was actually invalidated.
func (memo *Memo) Invalidate() bool {
defer memo.mu.Unlock()
memo.mu.Lock()
if memo.cache != nil && time.Now().Sub(memo.lastCache) > memo.cacheDur {
memo.cache = nil
memo.lastCache = time.Now()
return true
}
return false
}
func (memo *Memo) Get() (value interface{}, err error) {
memo.mu.Lock()
- if memo.cache == nil {
- memo.cache = &entry{ready: make(chan struct{})}
+ e := memo.cache
+ if e == nil {
+ e = &entry{ready: make(chan struct{})}
+ memo.cache = e
memo.mu.Unlock()
- memo.cache.res.value, memo.cache.res.err = memo.f()
+ e.res.value, e.res.err = memo.f()
- close(memo.cache.ready)
+ close(e.ready)
} else {
memo.mu.Unlock()
- <-memo.cache.ready
+ <-e.ready
}
- return memo.cache.res.value, memo.cache.res.err
+ return e.res.value, e.res.err
}
// Reset forcibly resets the cache to nil without checking if the cache
// duration has elapsed.
func (memo *Memo) Reset() {
defer memo.mu.Unlock()
memo.mu.Lock()
memo.cache = nil
memo.lastCache = time.Now()
}

File Metadata

Mime Type
text/x-diff
Expires
Tue, Dec 1, 3:53 PM (1 d, 11 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3831048

Event Timeline