Skip to content

Commit d70439b

Browse files
committed
add cache to resolve helpers
1 parent 4316de9 commit d70439b

4 files changed

Lines changed: 55 additions & 18 deletions

File tree

api/auth_middleware.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,20 @@ func (app *ApiServer) recoverAuthorityFromSignatureHeaders(c *fiber.Ctx) (int32,
3333
recoveredAddr := crypto.PubkeyToAddress(*publicKey)
3434
walletLower := strings.ToLower(recoveredAddr.Hex())
3535

36+
// check cache
37+
if hit, ok := app.resolveWalletCache.Get(walletLower); ok {
38+
return hit, walletLower
39+
}
40+
3641
var userId int32
3742
err = app.pool.QueryRow(
3843
c.Context(),
3944
`
40-
SELECT user_id FROM users
45+
SELECT user_id FROM users
4146
WHERE
42-
wallet = $1
43-
AND is_current = true
44-
ORDER BY handle_lc IS NOT NULL, created_at ASC
47+
wallet = $1
48+
AND is_current = true
49+
ORDER BY handle_lc IS NOT NULL, created_at ASC
4550
LIMIT 1
4651
`,
4752
walletLower,
@@ -51,6 +56,8 @@ func (app *ApiServer) recoverAuthorityFromSignatureHeaders(c *fiber.Ctx) (int32,
5156
return 0, walletLower
5257
}
5358

59+
app.resolveWalletCache.Set(walletLower, userId)
60+
5461
return userId, walletLower
5562
}
5663

api/server.go

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/gofiber/fiber/v2/middleware/cors"
1919
"github.com/gofiber/fiber/v2/middleware/recover"
2020
"github.com/jackc/pgx/v5/pgxpool"
21+
"github.com/maypok86/otter"
2122
"github.com/segmentio/encoding/json"
2223
"go.uber.org/zap"
2324
"go.uber.org/zap/zapcore"
@@ -68,19 +69,35 @@ func NewApiServer(config config.Config) *ApiServer {
6869

6970
pool, err := pgxpool.New(context.Background(), config.DbUrl)
7071
if err != nil {
71-
logger.Error("db connect failed", zap.Error(err))
72+
logger.Fatal("db connect failed", zap.Error(err))
73+
}
74+
75+
resolveHandleCache, err := otter.MustBuilder[string, int32](50_000).
76+
CollectStats().
77+
Build()
78+
if err != nil {
79+
panic(err)
80+
}
81+
82+
resolveWalletCache, err := otter.MustBuilder[string, int32](50_000).
83+
CollectStats().
84+
Build()
85+
if err != nil {
86+
panic(err)
7287
}
7388

7489
app := &ApiServer{
75-
fiber.New(fiber.Config{
90+
App: fiber.New(fiber.Config{
7691
JSONEncoder: json.Marshal,
7792
JSONDecoder: json.Unmarshal,
7893
ErrorHandler: errorHandler(logger),
7994
}),
80-
pool,
81-
dbv1.New(pool),
82-
logger,
83-
time.Now(),
95+
pool: pool,
96+
queries: dbv1.New(pool),
97+
logger: logger,
98+
started: time.Now(),
99+
resolveHandleCache: resolveHandleCache,
100+
resolveWalletCache: resolveWalletCache,
84101
}
85102

86103
app.Use(recover.New(recover.Config{
@@ -206,10 +223,12 @@ func NewApiServer(config config.Config) *ApiServer {
206223

207224
type ApiServer struct {
208225
*fiber.App
209-
pool *pgxpool.Pool
210-
queries *dbv1.Queries
211-
logger *zap.Logger
212-
started time.Time
226+
pool *pgxpool.Pool
227+
queries *dbv1.Queries
228+
logger *zap.Logger
229+
started time.Time
230+
resolveHandleCache otter.Cache[string, int32]
231+
resolveWalletCache otter.Cache[string, int32]
213232
}
214233

215234
func (app *ApiServer) home(c *fiber.Ctx) error {
@@ -234,10 +253,13 @@ func decodeIdList(c *fiber.Ctx) []int32 {
234253
}
235254

236255
func (app *ApiServer) resolveUserHandleToId(handle string) (int32, error) {
237-
// todo: can do some in memory cache here
256+
if hit, ok := app.resolveHandleCache.Get(handle); ok {
257+
return hit, nil
258+
}
238259
var userId int32
239260
sql := `select user_id from users where handle_lc = lower($1)`
240261
err := app.pool.QueryRow(context.Background(), sql, handle).Scan(&userId)
262+
app.resolveHandleCache.Set(handle, userId)
241263
return userId, err
242264
}
243265

go.mod

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,15 @@ require (
5757
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.4.0 // indirect
5858
github.com/dgraph-io/badger/v4 v4.5.1 // indirect
5959
github.com/dgraph-io/ristretto/v2 v2.1.0 // indirect
60+
github.com/dolthub/maphash v0.1.0 // indirect
6061
github.com/dustin/go-humanize v1.0.1 // indirect
6162
github.com/ethereum/c-kzg-4844 v1.0.0 // indirect
6263
github.com/ethereum/go-verkle v0.2.2 // indirect
6364
github.com/fatih/color v1.18.0 // indirect
6465
github.com/felixge/httpsnoop v1.0.4 // indirect
6566
github.com/fsnotify/fsnotify v1.7.0 // indirect
6667
github.com/gagliardetto/treeout v0.1.4 // indirect
68+
github.com/gammazero/deque v1.0.0 // indirect
6769
github.com/getsentry/sentry-go v0.27.0 // indirect
6870
github.com/go-gorp/gorp/v3 v3.1.0 // indirect
6971
github.com/go-kit/log v0.2.1 // indirect
@@ -98,7 +100,6 @@ require (
98100
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
99101
github.com/holiman/uint256 v1.3.2 // indirect
100102
github.com/iancoleman/strcase v0.3.0 // indirect
101-
github.com/holiman/uint256 v1.3.2 // indirect
102103
github.com/jackc/pgpassfile v1.0.0 // indirect
103104
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
104105
github.com/jackc/puddle/v2 v2.2.2 // indirect
@@ -117,6 +118,7 @@ require (
117118
github.com/mattn/go-colorable v0.1.14 // indirect
118119
github.com/mattn/go-isatty v0.0.20 // indirect
119120
github.com/mattn/go-runewidth v0.0.16 // indirect
121+
github.com/maypok86/otter v1.2.4 // indirect
120122
github.com/minio/highwayhash v1.0.3 // indirect
121123
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
122124
github.com/mitchellh/mapstructure v1.5.1-0.20231216201459-8508981c8b6c // indirect
@@ -148,11 +150,11 @@ require (
148150
github.com/streamingfast/logging v0.0.0-20230608130331-f22c91403091 // indirect
149151
github.com/supranational/blst v0.3.14 // indirect
150152
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 // indirect
151-
github.com/tklauser/go-sysconf v0.3.12 // indirect
152-
github.com/tklauser/numcpus v0.6.1 // indirect
153153
github.com/tidwall/gjson v1.18.0 // indirect
154154
github.com/tidwall/match v1.1.1 // indirect
155155
github.com/tidwall/pretty v1.2.1 // indirect
156+
github.com/tklauser/go-sysconf v0.3.12 // indirect
157+
github.com/tklauser/numcpus v0.6.1 // indirect
156158
github.com/valyala/bytebufferpool v1.0.0 // indirect
157159
github.com/valyala/fasttemplate v1.2.2 // indirect
158160
github.com/vektah/gqlparser/v2 v2.5.22 // indirect

go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ github.com/dgraph-io/badger/v4 v4.5.1 h1:7DCIXrQjo1LKmM96YD+hLVJ2EEsyyoWxJfpdd56
8787
github.com/dgraph-io/badger/v4 v4.5.1/go.mod h1:qn3Be0j3TfV4kPbVoK0arXCD1/nr1ftth6sbL5jxdoA=
8888
github.com/dgraph-io/ristretto/v2 v2.1.0 h1:59LjpOJLNDULHh8MC4UaegN52lC4JnO2dITsie/Pa8I=
8989
github.com/dgraph-io/ristretto/v2 v2.1.0/go.mod h1:uejeqfYXpUomfse0+lO+13ATz4TypQYLJZzBSAemuB4=
90+
github.com/dolthub/maphash v0.1.0 h1:bsQ7JsF4FkkWyrP3oCnFJgrCUAFbFf3kOl4L/QxPDyQ=
91+
github.com/dolthub/maphash v0.1.0/go.mod h1:gkg4Ch4CdCDu5h6PMriVLawB7koZ+5ijb9puGMV50a4=
9092
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
9193
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
9294
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
@@ -113,6 +115,8 @@ github.com/gagliardetto/solana-go v1.12.0 h1:rzsbilDPj6p+/DOPXBMLhwMZeBgeRuXjm5z
113115
github.com/gagliardetto/solana-go v1.12.0/go.mod h1:l/qqqIN6qJJPtxW/G1PF4JtcE3Zg2vD2EliZrr9Gn5k=
114116
github.com/gagliardetto/treeout v0.1.4 h1:ozeYerrLCmCubo1TcIjFiOWTTGteOOHND1twdFpgwaw=
115117
github.com/gagliardetto/treeout v0.1.4/go.mod h1:loUefvXTrlRG5rYmJmExNryyBRh8f89VZhmMOyCyqok=
118+
github.com/gammazero/deque v1.0.0 h1:LTmimT8H7bXkkCy6gZX7zNLtkbz4NdS2z8LZuor3j34=
119+
github.com/gammazero/deque v1.0.0/go.mod h1:iflpYvtGfM3U8S8j+sZEKIak3SAKYpA5/SQewgfXDKo=
116120
github.com/getsentry/sentry-go v0.27.0 h1:Pv98CIbtB3LkMWmXi4Joa5OOcwbmnX88sF5qbK3r3Ps=
117121
github.com/getsentry/sentry-go v0.27.0/go.mod h1:lc76E2QywIyW8WuBnwl8Lc4bkmQH4+w1gwTf25trprY=
118122
github.com/go-gorp/gorp/v3 v3.1.0 h1:ItKF/Vbuj31dmV4jxA1qblpSwkl9g1typ24xoe70IGs=
@@ -259,6 +263,8 @@ github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWE
259263
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
260264
github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc=
261265
github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
266+
github.com/maypok86/otter v1.2.4 h1:HhW1Pq6VdJkmWwcZZq19BlEQkHtI8xgsQzBVXJU0nfc=
267+
github.com/maypok86/otter v1.2.4/go.mod h1:mKLfoI7v1HOmQMwFgX4QkRk23mX6ge3RDvjdHOWG4R4=
262268
github.com/minio/highwayhash v1.0.3 h1:kbnuUMoHYyVl7szWjSxJnxw11k2U709jqFPPmIUyD6Q=
263269
github.com/minio/highwayhash v1.0.3/go.mod h1:GGYsuwP/fPD6Y9hMiXuapVvlIUEhFhMTh0rxU3ik1LQ=
264270
github.com/mitchellh/go-testing-interface v1.14.1 h1:jrgshOhYAUVNMAJiKbEu7EqAwgJJ2JqpQmpLJOu07cU=

0 commit comments

Comments
 (0)