mirror of
https://github.com/gopl-zh/gopl-zh.github.com.git
synced 2025-09-12 06:41:33 +00:00
good good study, day day up!
This commit is contained in:
61
vendor/gopl.io/ch9/memo4/memo.go
generated
vendored
Normal file
61
vendor/gopl.io/ch9/memo4/memo.go
generated
vendored
Normal file
@@ -0,0 +1,61 @@
|
||||
// Copyright © 2016 Alan A. A. Donovan & Brian W. Kernighan.
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
|
||||
// See page 276.
|
||||
|
||||
// Package memo provides a concurrency-safe memoization a function of
|
||||
// a function. Requests for different keys proceed in parallel.
|
||||
// Concurrent requests for the same key block until the first completes.
|
||||
// This implementation uses a Mutex.
|
||||
package memo
|
||||
|
||||
import "sync"
|
||||
|
||||
// Func is the type of the function to memoize.
|
||||
type Func func(string) (interface{}, error)
|
||||
|
||||
type result struct {
|
||||
value interface{}
|
||||
err error
|
||||
}
|
||||
|
||||
//!+
|
||||
type entry struct {
|
||||
res result
|
||||
ready chan struct{} // closed when res is ready
|
||||
}
|
||||
|
||||
func New(f Func) *Memo {
|
||||
return &Memo{f: f, cache: make(map[string]*entry)}
|
||||
}
|
||||
|
||||
type Memo struct {
|
||||
f Func
|
||||
mu sync.Mutex // guards cache
|
||||
cache map[string]*entry
|
||||
}
|
||||
|
||||
func (memo *Memo) Get(key string) (value interface{}, err error) {
|
||||
memo.mu.Lock()
|
||||
e := memo.cache[key]
|
||||
if e == nil {
|
||||
// This is the first request for this key.
|
||||
// This goroutine becomes responsible for computing
|
||||
// the value and broadcasting the ready condition.
|
||||
e = &entry{ready: make(chan struct{})}
|
||||
memo.cache[key] = e
|
||||
memo.mu.Unlock()
|
||||
|
||||
e.res.value, e.res.err = memo.f(key)
|
||||
|
||||
close(e.ready) // broadcast ready condition
|
||||
} else {
|
||||
// This is a repeat request for this key.
|
||||
memo.mu.Unlock()
|
||||
|
||||
<-e.ready // wait for ready condition
|
||||
}
|
||||
return e.res.value, e.res.err
|
||||
}
|
||||
|
||||
//!-
|
23
vendor/gopl.io/ch9/memo4/memo_test.go
generated
vendored
Normal file
23
vendor/gopl.io/ch9/memo4/memo_test.go
generated
vendored
Normal file
@@ -0,0 +1,23 @@
|
||||
// Copyright © 2016 Alan A. A. Donovan & Brian W. Kernighan.
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
|
||||
package memo_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"gopl.io/ch9/memo4"
|
||||
"gopl.io/ch9/memotest"
|
||||
)
|
||||
|
||||
var httpGetBody = memotest.HTTPGetBody
|
||||
|
||||
func Test(t *testing.T) {
|
||||
m := memo.New(httpGetBody)
|
||||
memotest.Sequential(t, m)
|
||||
}
|
||||
|
||||
func TestConcurrent(t *testing.T) {
|
||||
m := memo.New(httpGetBody)
|
||||
memotest.Concurrent(t, m)
|
||||
}
|
Reference in New Issue
Block a user