Skip to content

Commit a54f8da

Browse files
committed
cache added
1 parent 4f4e187 commit a54f8da

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

cmd/node/main.go

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"net/http"
1010
"os"
1111
"path"
12+
"strconv"
1213
"strings"
1314
"time"
1415

@@ -17,6 +18,7 @@ import (
1718
"github.com/go-chi/chi/v5"
1819
"github.com/go-chi/cors"
1920
"github.com/go-chi/render"
21+
lru "github.com/hashicorp/golang-lru/v2"
2022
bf "github.com/ipfs/boxo/files"
2123
bp "github.com/ipfs/boxo/path"
2224
"github.com/ipfs/go-cid"
@@ -82,9 +84,15 @@ func readUnixFile(ctx context.Context, rpc *kr.HttpApi, path string) (bf.File, e
8284
return file, nil
8385
}
8486

85-
func getSepFromId(ctx context.Context, kubo *kr.HttpApi, id string) (Sep, error) {
87+
func getSepFromId(ctx context.Context, kubo *kr.HttpApi, cache *lru.Cache[string, Sep], id string) (Sep, error) {
8688

8789
var sep Sep
90+
// check if id is in cache
91+
if v, ok := cache.Get(id); ok {
92+
log.Printf("Using cache for id: %s", id)
93+
return v, nil
94+
}
95+
8896
// collect the sep standard to retrieve content
8997
standard, err := readUnixFile(ctx, kubo, path.Join("/ipfs/", id))
9098
if err != nil {
@@ -101,13 +109,14 @@ func getSepFromId(ctx context.Context, kubo *kr.HttpApi, id string) (Sep, error)
101109
return Sep{}, err
102110
}
103111

112+
cache.Add(id, sep)
104113
return sep, nil
105114

106115
}
107116

108117
// TODO refactor all these to modules
109118

110-
func contentHandler(kubo *kr.HttpApi, eth *ec.Client) func(w http.ResponseWriter, r *http.Request) {
119+
func contentHandler(kubo *kr.HttpApi, eth *ec.Client, cache *lru.Cache[string, Sep]) func(w http.ResponseWriter, r *http.Request) {
111120
return func(w http.ResponseWriter, r *http.Request) {
112121
id := chi.URLParam(r, "id")
113122
sub := chi.URLParam(r, "sub")
@@ -126,7 +135,7 @@ func contentHandler(kubo *kr.HttpApi, eth *ec.Client) func(w http.ResponseWriter
126135

127136
log.Printf("Attempt to find id %s", id)
128137
// if the cid is a sep-001 standard switch the file served
129-
if sep, err := getSepFromId(ctx, kubo, c.String()); err == nil {
138+
if sep, err := getSepFromId(ctx, kubo, cache, c.String()); err == nil {
130139
log.Printf("Matched sep with id %s", id)
131140
id = sep.S.Cid
132141

@@ -155,7 +164,7 @@ func contentHandler(kubo *kr.HttpApi, eth *ec.Client) func(w http.ResponseWriter
155164
}
156165
}
157166

158-
func metaHandler(kubo *kr.HttpApi) func(w http.ResponseWriter, r *http.Request) {
167+
func metaHandler(kubo *kr.HttpApi, cache *lru.Cache[string, Sep]) func(w http.ResponseWriter, r *http.Request) {
159168
return func(w http.ResponseWriter, r *http.Request) {
160169
id := chi.URLParam(r, "id")
161170
// TODO refactor all this
@@ -171,7 +180,7 @@ func metaHandler(kubo *kr.HttpApi) func(w http.ResponseWriter, r *http.Request)
171180
}
172181

173182
log.Printf("Attempt to find id %s", id)
174-
sep, err := getSepFromId(ctx, kubo, id)
183+
sep, err := getSepFromId(ctx, kubo, cache, id)
175184
if err != nil {
176185
w.WriteHeader(http.StatusNotFound)
177186
return
@@ -228,9 +237,20 @@ func main() {
228237
log.Fatal(err)
229238
}
230239

231-
r.Get("/content/{id}/", contentHandler(kubo, client))
232-
r.Get("/content/{id}/{sub}", contentHandler(kubo, client))
233-
r.Get("/metadata/{id}/", metaHandler(kubo))
240+
cacheSize, err := strconv.Atoi(os.Getenv("LRU_CACHE_SIZE"))
241+
if err != nil {
242+
log.Fatal(err)
243+
}
244+
245+
lruCache, err := lru.New[string, Sep](cacheSize)
246+
if err != nil {
247+
log.Fatal(err)
248+
}
249+
250+
log.Printf("Cache initialized with size to %d", cacheSize)
251+
r.Get("/content/{id}/", contentHandler(kubo, client, lruCache))
252+
r.Get("/content/{id}/{sub}", contentHandler(kubo, client, lruCache))
253+
r.Get("/metadata/{id}/", metaHandler(kubo, lruCache))
234254
r.Get("/healthcheck/", health())
235255

236256
// Start the node on port 8080, and log any errors

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ require (
88
github.com/go-chi/chi/v5 v5.1.0
99
github.com/go-chi/cors v1.2.1
1010
github.com/go-chi/render v1.0.3
11+
github.com/hashicorp/golang-lru/v2 v2.0.7
1112
github.com/ipfs/boxo v0.24.3
1213
github.com/ipfs/go-cid v0.4.1
1314
github.com/ipfs/kubo v0.32.1
@@ -53,7 +54,6 @@ require (
5354
github.com/hashicorp/errwrap v1.1.0 // indirect
5455
github.com/hashicorp/go-multierror v1.1.1 // indirect
5556
github.com/hashicorp/golang-lru v1.0.2 // indirect
56-
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
5757
github.com/holiman/uint256 v1.3.1 // indirect
5858
github.com/huin/goupnp v1.3.0 // indirect
5959
github.com/ipfs/bbloom v0.0.4 // indirect

0 commit comments

Comments
 (0)