Last active
August 29, 2015 14:17
-
-
Save djherbis/450ac4dfd1c062593d25 to your computer and use it in GitHub Desktop.
Bad Cache Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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