Skip to content

Commit 013c72c

Browse files
api: cache responses (#74)
Co-authored-by: luke miles <[email protected]>
1 parent d48e6d7 commit 013c72c

File tree

3 files changed

+6
-6
lines changed

3 files changed

+6
-6
lines changed

api/proof.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,19 @@ func (s *Server) GetProof(w http.ResponseWriter, r *http.Request) {
5555
)
5656
if errors.Is(err, pgx.ErrNoRows) {
5757
s.sendJSONError(r, w, nil, http.StatusNotFound, "tree not found")
58+
w.Header().Set("Cache-Control", "public, max-age=60")
5859
return
5960
} else if err != nil {
6061
s.sendJSONError(r, w, err, http.StatusInternalServerError, "selecting proof")
6162
return
6263
}
6364

6465
// cache for 1 year if we're returning an unhashed leaf proof
66+
// or 60 seconds for an address proof
6567
if leaf != "" {
6668
w.Header().Set("Cache-Control", "public, max-age=31536000")
69+
} else {
70+
w.Header().Set("Cache-Control", "public, max-age=60")
6771
}
6872
s.sendJSON(r, w, resp)
6973
}

api/root.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"strings"
77

88
"github.com/ethereum/go-ethereum/common/hexutil"
9-
"github.com/jackc/pgx/v4"
109
)
1110

1211
func proofURLToDBQuery(param string) string {
@@ -54,15 +53,11 @@ func (s *Server) GetRoot(w http.ResponseWriter, r *http.Request) {
5453
roots := make([]hexutil.Bytes, 0)
5554
rb := make(hexutil.Bytes, 0)
5655

57-
_, err := s.db.QueryFunc(ctx, q, []interface{}{dbQuery}, []interface{}{&rb}, func(qfr pgx.QueryFuncRow) error {
58-
roots = append(roots, rb)
59-
return nil
60-
})
61-
6256
if err != nil {
6357
s.sendJSONError(r, w, err, http.StatusInternalServerError, "selecting root")
6458
return
6559
} else if len(roots) == 0 { // db.QueryFunc doesn't return pgx.ErrNoRows
60+
w.Header().Set("Cache-Control", "public, max-age=60")
6661
s.sendJSONError(r, w, nil, http.StatusNotFound, "root not found for proofs")
6762
return
6863
}

api/tree.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ func (s *Server) GetTree(w http.ResponseWriter, r *http.Request) {
190190
)
191191
if errors.Is(err, pgx.ErrNoRows) {
192192
s.sendJSONError(r, w, nil, http.StatusNotFound, "tree not found for root")
193+
w.Header().Set("Cache-Control", "public, max-age=60")
193194
return
194195
} else if err != nil {
195196
s.sendJSONError(r, w, err, http.StatusInternalServerError, "selecting tree")

0 commit comments

Comments
 (0)