Skip to content

Instantly share code, notes, and snippets.

@djherbis
Last active August 29, 2015 14:17
Show Gist options
  • Save djherbis/450ac4dfd1c062593d25 to your computer and use it in GitHub Desktop.
Save djherbis/450ac4dfd1c062593d25 to your computer and use it in GitHub Desktop.
Bad Cache Example
var mu sync.Mutex
var cache map[string][]byte
func Handler(w http.Response, r *http.Request) {
mu.Lock()
defer mu.Unlock()
url := r.URL.String()
p, ok := cache[url]
if !ok {
buf := bytes.NewBuffer(nil)
w := io.MultiWriter(w, buf)
io.Copy(w, expensiveProcessReader(url))
cache[url] = buf.Bytes()
} else {
w.Write(p)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment