mirror of
https://github.com/davegallant/rfd-fyi.git
synced 2025-08-06 07:13:39 +00:00
Cache topics
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
_ "github.com/joho/godotenv/autoload"
|
_ "github.com/joho/godotenv/autoload"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
@@ -31,6 +32,7 @@ type App struct {
|
|||||||
Router *mux.Router
|
Router *mux.Router
|
||||||
BasePath string
|
BasePath string
|
||||||
CurrentTopics []Topic
|
CurrentTopics []Topic
|
||||||
|
LastRefresh time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *App) Initialize() {
|
func (a *App) Initialize() {
|
||||||
@@ -53,9 +55,9 @@ func (a *App) initializeRoutes() {
|
|||||||
a.Router.HandleFunc("/topics", a.listTopics).Methods("GET")
|
a.Router.HandleFunc("/topics", a.listTopics).Methods("GET")
|
||||||
}
|
}
|
||||||
|
|
||||||
func respondWithError(w http.ResponseWriter, code int, message string) {
|
// func respondWithError(w http.ResponseWriter, code int, message string) {
|
||||||
respondWithJSON(w, code, map[string]string{"error": message})
|
// respondWithJSON(w, code, map[string]string{"error": message})
|
||||||
}
|
// }
|
||||||
|
|
||||||
func respondWithJSON(w http.ResponseWriter, code int, payload interface{}) {
|
func respondWithJSON(w http.ResponseWriter, code int, payload interface{}) {
|
||||||
response, _ := json.Marshal(payload)
|
response, _ := json.Marshal(payload)
|
||||||
@@ -72,16 +74,20 @@ func respondWithJSON(w http.ResponseWriter, code int, payload interface{}) {
|
|||||||
// @Router /topics [get]
|
// @Router /topics [get]
|
||||||
// @Success 200 {array} Topic
|
// @Success 200 {array} Topic
|
||||||
func (a *App) listTopics(w http.ResponseWriter, r *http.Request) {
|
func (a *App) listTopics(w http.ResponseWriter, r *http.Request) {
|
||||||
var topics []Topic
|
if time.Since(a.LastRefresh).Minutes() > 1 {
|
||||||
a.refreshDeals()
|
a.refreshTopics()
|
||||||
respondWithJSON(w, http.StatusOK, topics)
|
} 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)
|
latestTopics := a.getDeals(9, 1, 4)
|
||||||
// TODO: only drop deals if a timer has been met
|
// TODO: only drop deals if a timer has been met
|
||||||
log.Debug().Msg("Refreshing deals")
|
log.Debug().Msg("Refreshing topics")
|
||||||
a.CurrentTopics = latestTopics
|
a.CurrentTopics = latestTopics
|
||||||
|
a.LastRefresh = time.Now()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *App) getDeals(id int, firstPage int, lastPage int) []Topic {
|
func (a *App) getDeals(id int, firstPage int, lastPage int) []Topic {
|
||||||
|
@@ -12,7 +12,8 @@
|
|||||||
<tr>
|
<tr>
|
||||||
<th scope="col">Deal</th>
|
<th scope="col">Deal</th>
|
||||||
<th scope="col">Views</th>
|
<th scope="col">Views</th>
|
||||||
<th scope="col">Score</th>
|
<!-- TODO:score -->
|
||||||
|
<!-- <th scope="col">Score</th> -->
|
||||||
<th scope="col">Last Post</th>
|
<th scope="col">Last Post</th>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
|
Reference in New Issue
Block a user