From 83d143805ad7cc75bbe1640bb06840d1149d5ed6 Mon Sep 17 00:00:00 2001 From: Umputun Date: Tue, 26 Dec 2017 23:03:26 -0600 Subject: [PATCH] cache for per-user comments --- app/rest/server.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/app/rest/server.go b/app/rest/server.go index b8b2c55b..18c5fb5e 100644 --- a/app/rest/server.go +++ b/app/rest/server.go @@ -253,14 +253,23 @@ func (s *Server) findUserCommentsCtrl(w http.ResponseWriter, r *http.Request) { log.Printf("[DEBUG] get comments by userID %s", userID) - comment, err := s.Store.GetForUser(store.Locator{}, userID) + cacheKey := r.URL.String() + if comments, ok := s.respCache.Get(cacheKey); ok { + log.Printf("[DEBUG] cash hit for %s", cacheKey) + renderJSONWithHTML(w, r, comments) + return + } + + comments, err := s.Store.GetForUser(store.Locator{}, userID) if err != nil { log.Printf("[WARN] can't get comment, %s", err) httpError(w, r, http.StatusBadRequest, err, "can't get comment by user id") return } + + s.respCache.Set(cacheKey, comments, time.Hour) render.Status(r, http.StatusOK) - renderJSONWithHTML(w, r, comment) + renderJSONWithHTML(w, r, comments) } // GET /user