Page Menu
Home
Musing Studio
Search
Configure Global Search
Log In
Files
F10668894
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
1 KB
Subscribers
None
View Options
diff --git a/memo/memo.go b/memo/memo.go
index 8592b17..a31c8be 100644
--- a/memo/memo.go
+++ b/memo/memo.go
@@ -1,56 +1,69 @@
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) *Memo {
+func New(f Func, cd time.Duration) *Memo {
return &Memo{
- f: f,
+ f: f,
+ cacheDur: cd,
}
}
-func (memo *Memo) Invalidate() {
+// 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()
- memo.cache = nil
- memo.mu.Unlock()
+
+ 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{})}
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
}
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Thu, May 15, 11:06 AM (22 h, 13 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3239673
Attached To
rWC Write.as Web Core
Event Timeline
Log In to Comment