- Install the github.com/patrickmn/go-cache package using the go get command, as follows:
$ go get github.com/patrickmn/go-cache
- 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 ...