From c21bc1baa9e85b58bf09f601e5bb6f894234ae79 Mon Sep 17 00:00:00 2001 From: melvandito Date: Fri, 9 Oct 2020 16:49:38 +0700 Subject: [PATCH] test --- main.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/main.go b/main.go index 328ebfe..75aace2 100644 --- a/main.go +++ b/main.go @@ -5,15 +5,39 @@ import ( "io/ioutil" "log" "net/http" + "time" + + "github.com/patrickmn/go-cache" ) +var Kes *cache.Cache + +type Response struct { + Message string `json:"message"` +} + func github(w http.ResponseWriter, r *http.Request) { + value, found := Kes.Get("status") + if found { + json.NewEncoder(w).Encode(value) + 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() + + var response Response + err = json.Unmarshal(data, &response) + if err != nil { + json.NewEncoder(w).Encode(err) + } + + Kes.Set("status", response, cache.DefaultExpiration) + json.NewEncoder(w).Encode(string(data)) } @@ -23,5 +47,6 @@ func handleRequests() { } func main() { + Kes = cache.New(2*time.Minute, 1*time.Minute) handleRequests() }