From c280b6ebf31d8395e7dfdcbe1b46083d915fcf80 Mon Sep 17 00:00:00 2001 From: Mulyadi Setiawan Date: Fri, 9 Oct 2020 16:37:59 +0700 Subject: [PATCH] asd --- main.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/main.go b/main.go index 328ebfe..9c53edf 100644 --- a/main.go +++ b/main.go @@ -2,18 +2,34 @@ package main import ( "encoding/json" + "fmt" "io/ioutil" "log" "net/http" + "time" + + cache "github.com/patrickmn/go-cache" ) +var gocache *cache.Cache + func github(w http.ResponseWriter, r *http.Request) { + res, found := gocache.Get("status") + fmt.Println("res, found", res, found) + if found { + json.NewEncoder(w).Encode(res) + return + } + resp, err := http.Get("https://api.github.com/status") if err != nil { json.NewEncoder(w).Encode(err) } data, _ := ioutil.ReadAll(resp.Body) resp.Body.Close() + + fmt.Println("SET CACHE", string(data)) + gocache.Set("status", string(data), 10*time.Second) json.NewEncoder(w).Encode(string(data)) } @@ -23,5 +39,7 @@ func handleRequests() { } func main() { + gocache = cache.New(5*time.Second, 10*time.Second) + fmt.Println("RUNNING") handleRequests() }