From b059b01a0b868ecf32350cee5be9954a9e90920c Mon Sep 17 00:00:00 2001 From: Dave Gallant Date: Sun, 21 Aug 2022 14:47:34 +0000 Subject: [PATCH] Cache topics --- backend/app.go | 22 ++++++++++++++-------- src/App.vue | 3 ++- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/backend/app.go b/backend/app.go index 2be3307..899148d 100644 --- a/backend/app.go +++ b/backend/app.go @@ -5,6 +5,7 @@ import ( "fmt" "io/ioutil" "net/http" + "time" _ "github.com/joho/godotenv/autoload" "github.com/rs/zerolog/log" @@ -31,6 +32,7 @@ type App struct { Router *mux.Router BasePath string CurrentTopics []Topic + LastRefresh time.Time } func (a *App) Initialize() { @@ -53,9 +55,9 @@ func (a *App) initializeRoutes() { a.Router.HandleFunc("/topics", a.listTopics).Methods("GET") } -func respondWithError(w http.ResponseWriter, code int, message string) { - respondWithJSON(w, code, map[string]string{"error": message}) -} +// func respondWithError(w http.ResponseWriter, code int, message string) { +// respondWithJSON(w, code, map[string]string{"error": message}) +// } func respondWithJSON(w http.ResponseWriter, code int, payload interface{}) { response, _ := json.Marshal(payload) @@ -72,16 +74,20 @@ func respondWithJSON(w http.ResponseWriter, code int, payload interface{}) { // @Router /topics [get] // @Success 200 {array} Topic func (a *App) listTopics(w http.ResponseWriter, r *http.Request) { - var topics []Topic - a.refreshDeals() - respondWithJSON(w, http.StatusOK, topics) + if time.Since(a.LastRefresh).Minutes() > 1 { + a.refreshTopics() + } else { + log.Debug().Msg("Topics cache has not expired. Using existing.") + } + respondWithJSON(w, http.StatusOK, a.CurrentTopics) } -func (a *App) refreshDeals() { +func (a *App) refreshTopics() { latestTopics := a.getDeals(9, 1, 4) // TODO: only drop deals if a timer has been met - log.Debug().Msg("Refreshing deals") + log.Debug().Msg("Refreshing topics") a.CurrentTopics = latestTopics + a.LastRefresh = time.Now() } func (a *App) getDeals(id int, firstPage int, lastPage int) []Topic { diff --git a/src/App.vue b/src/App.vue index 7d51d77..259a7a7 100644 --- a/src/App.vue +++ b/src/App.vue @@ -12,7 +12,8 @@ Deal Views - Score + + Last Post