How to do it...

  1. Install the github.com/patrickmn/go-cache package using the go get command, as follows:
$ go get github.com/patrickmn/go-cache
  1. Create http-caching.go, where we will create a cache and populate it with data on server boot up, as follows:
package mainimport (  "fmt"  "log"  "net/http"  "time"  "github.com/patrickmn/go-cache")const (  CONN_HOST = "localhost"  CONN_PORT = "8080")var newCache *cache.Cachefunc init() {  newCache = cache.New(5*time.Minute, 10*time.Minute)  newCache.Set("foo", "bar", cache.DefaultExpiration)}func getFromCache(w http.ResponseWriter, r *http.Request) {  foo, found := newCache.Get("foo")  if found   {    log.Print("Key Found in Cache with value as :: ",     foo.(string))    fmt.Fprintf(w, "Hello "+foo.(string))  }  else ...

Get Go Web Development Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.