mirror of
https://github.com/gopl-zh/gopl-zh.github.com.git
synced 2024-11-05 05:53:45 +00:00
49 lines
1.0 KiB
Go
49 lines
1.0 KiB
Go
// 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
|
|
// type Func. Requests for different keys run concurrently.
|
|
// Concurrent requests for the same key result in duplicate work.
|
|
package memo
|
|
|
|
import "sync"
|
|
|
|
type Memo struct {
|
|
f Func
|
|
mu sync.Mutex // guards cache
|
|
cache map[string]result
|
|
}
|
|
|
|
type Func func(string) (interface{}, error)
|
|
|
|
type result struct {
|
|
value interface{}
|
|
err error
|
|
}
|
|
|
|
func New(f Func) *Memo {
|
|
return &Memo{f: f, cache: make(map[string]result)}
|
|
}
|
|
|
|
//!+
|
|
|
|
func (memo *Memo) Get(key string) (value interface{}, err error) {
|
|
memo.mu.Lock()
|
|
res, ok := memo.cache[key]
|
|
memo.mu.Unlock()
|
|
if !ok {
|
|
res.value, res.err = memo.f(key)
|
|
|
|
// Between the two critical sections, several goroutines
|
|
// may race to compute f(key) and update the map.
|
|
memo.mu.Lock()
|
|
memo.cache[key] = res
|
|
memo.mu.Unlock()
|
|
}
|
|
return res.value, res.err
|
|
}
|
|
|
|
//!-
|