Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions cache/cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package cache

import (
"fmt"
"time"

"github.com/patrickmn/go-cache"
)

var Cache = cache.New(5*time.Minute, 5*time.Minute)

const (
GithubMessageCacheKey = "github_message"
)

func SetCache(key string, data interface{}) bool {
fmt.Println("SET CACHE! KEY: ", key, " DATA: ", data)
Cache.Set(key, data, 1*time.Minute)
return true
}
func GetCache(key string) (string, bool) {
var (
data string
found bool
)
result, found := Cache.Get(key)
if found {
data = result.(string)
fmt.Println("CACHE FOUND! KEY", key, " DATA: ", data)
}
return data, found
}
15 changes: 13 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,37 @@ package main

import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"

"github.com/cache-in-go/cache"
)

func github(w http.ResponseWriter, r *http.Request) {
// get cache
cacheData, found := cache.GetCache(cache.GithubMessageCacheKey)
if found && cacheData != "" {
json.NewEncoder(w).Encode(cacheData)
return
}
fmt.Println("GET GITHUB STATUS")
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("DATA: ", string(data))
// set mapcache
cache.SetCache(cache.GithubMessageCacheKey, string(data))
json.NewEncoder(w).Encode(string(data))
}

func handleRequests() {
http.HandleFunc("/github", github)
log.Fatal(http.ListenAndServe(":10000", nil))
}

func main() {
handleRequests()
}