O'Reilly logo

Go Web Development Cookbook by Arpit Aggarwal

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required