Skip to content
Switch branches/tags
Go to file
Cannot retrieve contributors at this time
61 lines (48 sloc) 1.39 KB
// Copyright © 2016 Alan A. A. Donovan & Brian W. Kernighan.
// License:
// 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) {
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
e.res.value, e.res.err = memo.f(key)
close(e.ready) // broadcast ready condition
} else {
// This is a repeat request for this key.
<-e.ready // wait for ready condition
return e.res.value, e.res.err