From 51d0e3eb8a6f8384c9f86747c930f2526d73b06e Mon Sep 17 00:00:00 2001 From: Umputun Date: Mon, 8 Jan 2018 19:43:37 -0600 Subject: [PATCH] cache listCtrl response --- app/rest/server.go | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/app/rest/server.go b/app/rest/server.go index 225bad02..9b1ad176 100644 --- a/app/rest/server.go +++ b/app/rest/server.go @@ -334,12 +334,21 @@ func (s *Server) countCtrl(w http.ResponseWriter, r *http.Request) { // GET /list?site=siteID - list posts with comments func (s *Server) listCtrl(w http.ResponseWriter, r *http.Request) { - posts, err := s.DataService.List(r.URL.Query().Get("site")) + + siteID := r.URL.Query().Get("site") + data, err := s.respCache.get(r.URL.String(), time.Hour, func() ([]byte, error) { + posts, e := s.DataService.List(siteID) + if e != nil { + return nil, e + } + return encodeJSONWithHTML(posts) + }) + if err != nil { - common.SendErrorJSON(w, r, http.StatusBadRequest, err, "can't get count") + common.SendErrorJSON(w, r, http.StatusBadRequest, err, "can't get list of comments for "+siteID) return } - render.JSON(w, r, JSON{"posts": posts, "site": r.URL.Query().Get("site")}) + renderJSONFromBytes(w, r, data) } // PUT /vote/{id}?site=siteID&url=post-url&vote=1 - vote for/against comment