From db52f60a9a8fa212bb59535aa2053c7a2539899a Mon Sep 17 00:00:00 2001 From: rakadityastok Date: Fri, 9 Oct 2020 16:33:16 +0700 Subject: [PATCH 1/2] freecache --- go.mod | 5 +++++ go.sum | 8 ++++++++ main.go | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+) create mode 100644 go.mod create mode 100644 go.sum diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..c6fc958 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module github.com/rakadityas/cache-in-go + +go 1.14 + +require github.com/coocood/freecache v1.1.1 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..3c0f699 --- /dev/null +++ b/go.sum @@ -0,0 +1,8 @@ +github.com/OneOfOne/xxhash v1.2.2 h1:KMrpdQIwFcEqXDklaen+P1axHaj9BSKzvpUUfnHldSE= +github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= +github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko= +github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= +github.com/coocood/freecache v1.1.1 h1:uukNF7QKCZEdZ9gAV7WQzvh0SbjwdMF6m3x3rxEkaPc= +github.com/coocood/freecache v1.1.1/go.mod h1:OKrEjkGVoxZhyWAJoeFi5BMLUJm2Tit0kpGkIr7NGYY= +github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72 h1:qLC7fQah7D6K1B0ujays3HV9gkFtllcxhzImRR7ArPQ= +github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= diff --git a/main.go b/main.go index 328ebfe..3d5ee2e 100644 --- a/main.go +++ b/main.go @@ -2,26 +2,89 @@ package main import ( "encoding/json" + "fmt" "io/ioutil" "log" "net/http" + + "github.com/coocood/freecache" ) +var cache *freecache.Cache + func github(w http.ResponseWriter, r *http.Request) { + + cacheVal, err := getCache([]byte("status")) + if err != nil { + json.NewEncoder(w).Encode(err) + return + } + + if len(cacheVal) != 0 { + json.NewEncoder(w).Encode(string(cacheVal)) + 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() + + err = saveCache([]byte("status"), data, 10) + if err != nil { + json.NewEncoder(w).Encode(err) + return + } + json.NewEncoder(w).Encode(string(data)) } +func initCache() { + cache = freecache.NewCache(10 * 1024 * 1024) +} + +func saveCache(key []byte, value []byte, expireSeconds int) (err error) { + fmt.Println("[SAVE CACHE]") + return cache.Set(key, value, expireSeconds) +} + +func getCache(key []byte) ([]byte, error) { + fmt.Println("[GET CACHE]") + return cache.Get(key) +} + func handleRequests() { http.HandleFunc("/github", github) log.Fatal(http.ListenAndServe(":10000", nil)) } func main() { + + initCache() + handleRequests() } + +/** + -. data apa yg dibalikin + curl --location --request GET 'localhost:10000/github' + + pola data: + - response + - "{\"message\":\"GitHub lives! (2020-10-09 02:06:14 -0700) (1)\"}" + + - berubah per 10 detik + - balikan satu response (general) + + -. cache yang mana + - freecache + + - keynya by apa + - public key + + -. kenapa pilih cache itu + - + +**/ From 561c7c7910cd57bb64110ff20c71ff4733c5d52a Mon Sep 17 00:00:00 2001 From: rakadityastok Date: Fri, 9 Oct 2020 16:35:38 +0700 Subject: [PATCH 2/2] revision --- main.go | 1 - 1 file changed, 1 deletion(-) diff --git a/main.go b/main.go index 3d5ee2e..5c35552 100644 --- a/main.go +++ b/main.go @@ -17,7 +17,6 @@ func github(w http.ResponseWriter, r *http.Request) { cacheVal, err := getCache([]byte("status")) if err != nil { json.NewEncoder(w).Encode(err) - return } if len(cacheVal) != 0 {