《GO程序设计语言》设计中案例,仅作为笔记进行收藏。并发非阻塞的缓存系统案例,它能解决函数记忆问题,即缓存函数的结果,达到多次调用但只须计算一次的效果。此案例通信顺序进程构建。
package memotest
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"sync"
"testing"
"time"
)
func httpGetBody(url string) (interface{}, error) {
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
return ioutil.ReadAll(resp.Body)
}
var HTTPGetBody = httpGetBody
func incomingURLs() <-chan string {
ch := make(chan string)
go func() {
for _, url := range []string{
"https://golang.org",
"https://godoc.org",
"https://play.golang.org",
"http://gopl.io",
"https://golang.org",
"https://godoc.org",
"https://play.golang.org",
"http://gopl.io",
} {
ch <- url
}
close(ch)
}()
return ch
}
type M interface {
Get(key string) (interface{}, error)
}
func Sequential(t *testing.T, m M) {
for url := range incomingURLs() {
start := time.Now()
value, err := m.Get(url)
if err != nil {
log.Print(err)
continue
}
fmt.Printf("%s, %s, %d bytes\n",
url, time.Since(start), len(value.([]byte)))
}
}
func Concurrent(t *testing.T, m M) {
var n sync.WaitGroup
for url := range incomingURLs() {
n.Add(1)
go func(url string) {
defer n.Done()
start := time.Now()
value, err := m.Get(url)
if err != nil {
log.Print(err)
return
}
fmt.Printf("%s, %s, %d bytes\n",
url, time.Since(start), len(value.([]byte)))
}(url)
}
n.Wait()
}
package memo
// Func 是用于记忆的函数类型
type Func func(string) (interface{}, error)
type result struct {
value interface{}
err error
}
// 解决重复抑制的问题
type entry struct {
res result
ready chan struct{} // res 准备好后会被关闭
}
// request 是一条请求消息,key 需要用Func来调用
type request struct {
key string
response chan<- result // 客户端端需要单个 result
}
type Memo struct{ requests chan request }
// New 返回f的函数记忆,客户端之后需要调用 Close
func New(f Func) *Memo {
memo := &Memo{requests: make(chan request)}
go memo.server(f)
return memo
}
func (memo *Memo) Get(key string) (interface{}, error) {
response := make(chan result)
memo.requests <- request{key, response}
res := <-response
return res.value, res.err
}
func (memo *Memo) Close() { close(memo.requests) }
// 监控goroutine中的cache变量
func (memo *Memo) server(f Func) {
cache := make(map[string]*entry)
for req := range memo.requests {
e := cache[req.key]
if e == nil {
// 对这个key的第一次请求
e = &entry{ready: make(chan struct{})}
cache[req.key] = e
// 调用 f(key)
go e.call(f, req.key)
}
go e.deliver(req.response)
}
}
func (e *entry) call(f Func, key string) {
// 执行函数
e.res.value, e.res.err = f(key)
// 通知数据已准备完毕
close(e.ready)
}
func (e *entry) deliver(response chan<- result) {
// 等待数据准备完毕
<-e.ready
// 向客户端发送结果
response <- e.res
}
package memo
import (
"testing"
"main/memo"
"main/memotest"
)
var httpGetBody = memotest.HTTPGetBody
func Test(t *testing.T) {
m := memo.New(httpGetBody)
defer m.Close()
memotest.Sequential(t, m)
}
func TestConcurrent(t *testing.T) {
m := memo.New(httpGetBody)
defer m.Close()
memotest.Concurrent(t, m)
}