Skip to content

Instantly share code, notes, and snippets.

@jonbodner
Created December 28, 2017 20:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonbodner/ab468310f2670737ba4fe277b5b143ac to your computer and use it in GitHub Desktop.
Save jonbodner/ab468310f2670737ba4fe277b5b143ac to your computer and use it in GitHub Desktop.
func AddSlowly(a, b int) int {
time.Sleep(100 * time.Millisecond)
return a + b
}
func main() {
ch, err := Cacher(AddSlowly, 2*time.Second)
if err != nil {
panic(err)
}
chAddSlowly := ch.(func(int, int) int)
for i := 0; i < 5; i++ {
start := time.Now()
result := chAddSlowly(1, 2)
end := time.Now()
fmt.Println("got result", result, "in", end.Sub(start))
}
time.Sleep(3 * time.Second)
start := time.Now()
result := chAddSlowly(1, 2)
end := time.Now()
fmt.Println("got result", result, "in", end.Sub(start))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment