From 013c72cf804557a1b188906e5f0bdd442e76ab0a Mon Sep 17 00:00:00 2001 From: Malone Hedges Date: Mon, 26 Sep 2022 02:58:50 -0700 Subject: [PATCH] api: cache responses (#74) Co-authored-by: luke miles --- api/proof.go | 4 ++++ api/root.go | 7 +------ api/tree.go | 1 + 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/api/proof.go b/api/proof.go index adba7b9..243cef0 100644 --- a/api/proof.go +++ b/api/proof.go @@ -55,6 +55,7 @@ func (s *Server) GetProof(w http.ResponseWriter, r *http.Request) { ) if errors.Is(err, pgx.ErrNoRows) { s.sendJSONError(r, w, nil, http.StatusNotFound, "tree not found") + w.Header().Set("Cache-Control", "public, max-age=60") return } else if err != nil { s.sendJSONError(r, w, err, http.StatusInternalServerError, "selecting proof") @@ -62,8 +63,11 @@ func (s *Server) GetProof(w http.ResponseWriter, r *http.Request) { } // cache for 1 year if we're returning an unhashed leaf proof + // or 60 seconds for an address proof if leaf != "" { w.Header().Set("Cache-Control", "public, max-age=31536000") + } else { + w.Header().Set("Cache-Control", "public, max-age=60") } s.sendJSON(r, w, resp) } diff --git a/api/root.go b/api/root.go index 3590cdf..f6228aa 100644 --- a/api/root.go +++ b/api/root.go @@ -6,7 +6,6 @@ import ( "strings" "github.com/ethereum/go-ethereum/common/hexutil" - "github.com/jackc/pgx/v4" ) func proofURLToDBQuery(param string) string { @@ -54,15 +53,11 @@ func (s *Server) GetRoot(w http.ResponseWriter, r *http.Request) { roots := make([]hexutil.Bytes, 0) rb := make(hexutil.Bytes, 0) - _, err := s.db.QueryFunc(ctx, q, []interface{}{dbQuery}, []interface{}{&rb}, func(qfr pgx.QueryFuncRow) error { - roots = append(roots, rb) - return nil - }) - if err != nil { s.sendJSONError(r, w, err, http.StatusInternalServerError, "selecting root") return } else if len(roots) == 0 { // db.QueryFunc doesn't return pgx.ErrNoRows + w.Header().Set("Cache-Control", "public, max-age=60") s.sendJSONError(r, w, nil, http.StatusNotFound, "root not found for proofs") return } diff --git a/api/tree.go b/api/tree.go index 000529f..28915a5 100644 --- a/api/tree.go +++ b/api/tree.go @@ -190,6 +190,7 @@ func (s *Server) GetTree(w http.ResponseWriter, r *http.Request) { ) if errors.Is(err, pgx.ErrNoRows) { s.sendJSONError(r, w, nil, http.StatusNotFound, "tree not found for root") + w.Header().Set("Cache-Control", "public, max-age=60") return } else if err != nil { s.sendJSONError(r, w, err, http.StatusInternalServerError, "selecting tree")