Skip to content

Commit a66db5c

Browse files
authored
add cache to resolve helpers (#37)
1 parent c0ac99f commit a66db5c

4 files changed

Lines changed: 53 additions & 15 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
@@ -20,6 +20,7 @@ import (
2020
pgxzap "github.com/jackc/pgx-zap"
2121
"github.com/jackc/pgx/v5/pgxpool"
2222
"github.com/jackc/pgx/v5/tracelog"
23+
"github.com/maypok86/otter"
2324
"github.com/segmentio/encoding/json"
2425
"go.uber.org/zap"
2526
"go.uber.org/zap/zapcore"
@@ -80,19 +81,35 @@ func NewApiServer(config config.Config) *ApiServer {
8081

8182
pool, err := pgxpool.NewWithConfig(context.Background(), connConfig)
8283
if err != nil {
83-
logger.Error("db connect failed", zap.Error(err))
84+
logger.Fatal("db connect failed", zap.Error(err))
85+
}
86+
87+
resolveHandleCache, err := otter.MustBuilder[string, int32](50_000).
88+
CollectStats().
89+
Build()
90+
if err != nil {
91+
panic(err)
92+
}
93+
94+
resolveWalletCache, err := otter.MustBuilder[string, int32](50_000).
95+
CollectStats().
96+
Build()
97+
if err != nil {
98+
panic(err)
8499
}
85100

86101
app := &ApiServer{
87-
fiber.New(fiber.Config{
102+
App: fiber.New(fiber.Config{
88103
JSONEncoder: json.Marshal,
89104
JSONDecoder: json.Unmarshal,
90105
ErrorHandler: errorHandler(logger),
91106
}),
92-
pool,
93-
dbv1.New(pool),
94-
logger,
95-
time.Now(),
107+
pool: pool,
108+
queries: dbv1.New(pool),
109+
logger: logger,
110+
started: time.Now(),
111+
resolveHandleCache: resolveHandleCache,
112+
resolveWalletCache: resolveWalletCache,
96113
}
97114

98115
app.Use(recover.New(recover.Config{
@@ -218,10 +235,12 @@ func NewApiServer(config config.Config) *ApiServer {
218235

219236
type ApiServer struct {
220237
*fiber.App
221-
pool *pgxpool.Pool
222-
queries *dbv1.Queries
223-
logger *zap.Logger
224-
started time.Time
238+
pool *pgxpool.Pool
239+
queries *dbv1.Queries
240+
logger *zap.Logger
241+
started time.Time
242+
resolveHandleCache otter.Cache[string, int32]
243+
resolveWalletCache otter.Cache[string, int32]
225244
}
226245

227246
func (app *ApiServer) home(c *fiber.Ctx) error {
@@ -246,10 +265,13 @@ func decodeIdList(c *fiber.Ctx) []int32 {
246265
}
247266

248267
func (app *ApiServer) resolveUserHandleToId(handle string) (int32, error) {
249-
// todo: can do some in memory cache here
268+
if hit, ok := app.resolveHandleCache.Get(handle); ok {
269+
return hit, nil
270+
}
250271
var userId int32
251272
sql := `select user_id from users where handle_lc = lower($1)`
252273
err := app.pool.QueryRow(context.Background(), sql, handle).Scan(&userId)
274+
app.resolveHandleCache.Set(handle, userId)
253275
return userId, err
254276
}
255277

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ require (
1313
github.com/jackc/pgx-zap v0.0.0-20221202020421-94b1cb2f889f
1414
github.com/jackc/pgx/v5 v5.7.4
1515
github.com/joho/godotenv v1.5.1
16+
github.com/maypok86/otter v1.2.4
1617
github.com/segmentio/encoding v0.4.1
1718
github.com/speps/go-hashids/v2 v2.0.1
1819
github.com/stretchr/testify v1.10.0
@@ -33,9 +34,11 @@ require (
3334
github.com/cosmos/gogoproto v1.7.0 // indirect
3435
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
3536
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.4.0 // indirect
37+
github.com/dolthub/maphash v0.1.0 // indirect
3638
github.com/fatih/color v1.18.0 // indirect
3739
github.com/felixge/httpsnoop v1.0.4 // indirect
3840
github.com/gagliardetto/treeout v0.1.4 // indirect
41+
github.com/gammazero/deque v1.0.0 // indirect
3942
github.com/go-kit/log v0.2.1 // indirect
4043
github.com/go-logfmt/logfmt v0.6.0 // indirect
4144
github.com/go-logr/logr v1.4.2 // indirect

go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ github.com/decred/dcrd/crypto/blake256 v1.1.0 h1:zPMNGQCm0g4QTY27fOCorQW7EryeQ/U
2727
github.com/decred/dcrd/crypto/blake256 v1.1.0/go.mod h1:2OfgNZ5wDpcsFmHmCK5gZTPcCXqlm2ArzUIkw9czNJo=
2828
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.4.0 h1:NMZiJj8QnKe1LgsbDayM4UoHwbvwDRwnI3hwNaAHRnc=
2929
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.4.0/go.mod h1:ZXNYxsqcloTdSy/rNShjYzMhyjf0LaoftYK0p+A3h40=
30+
github.com/dolthub/maphash v0.1.0 h1:bsQ7JsF4FkkWyrP3oCnFJgrCUAFbFf3kOl4L/QxPDyQ=
31+
github.com/dolthub/maphash v0.1.0/go.mod h1:gkg4Ch4CdCDu5h6PMriVLawB7koZ+5ijb9puGMV50a4=
3032
github.com/ethereum/go-ethereum v1.15.8 h1:H6NilvRXFVoHiXZ3zkuTqKW5XcxjLZniV5UjxJt1GJU=
3133
github.com/ethereum/go-ethereum v1.15.8/go.mod h1:+S9k+jFzlyVTNcYGvqFhzN/SFhI6vA+aOY4T5tLSPL0=
3234
github.com/fatih/color v1.18.0 h1:S8gINlzdQ840/4pfAwic/ZE0djQEH3wM94VfqLTZcOM=
@@ -39,6 +41,8 @@ github.com/gagliardetto/solana-go v1.12.0 h1:rzsbilDPj6p+/DOPXBMLhwMZeBgeRuXjm5z
3941
github.com/gagliardetto/solana-go v1.12.0/go.mod h1:l/qqqIN6qJJPtxW/G1PF4JtcE3Zg2vD2EliZrr9Gn5k=
4042
github.com/gagliardetto/treeout v0.1.4 h1:ozeYerrLCmCubo1TcIjFiOWTTGteOOHND1twdFpgwaw=
4143
github.com/gagliardetto/treeout v0.1.4/go.mod h1:loUefvXTrlRG5rYmJmExNryyBRh8f89VZhmMOyCyqok=
44+
github.com/gammazero/deque v1.0.0 h1:LTmimT8H7bXkkCy6gZX7zNLtkbz4NdS2z8LZuor3j34=
45+
github.com/gammazero/deque v1.0.0/go.mod h1:iflpYvtGfM3U8S8j+sZEKIak3SAKYpA5/SQewgfXDKo=
4246
github.com/go-kit/log v0.2.1 h1:MRVx0/zhvdseW+Gza6N9rVzU/IVzaeE1SFI4raAhmBU=
4347
github.com/go-kit/log v0.2.1/go.mod h1:NwTd00d/i8cPZ3xOwwiv2PO5MOcx78fFErGNcVmBjv0=
4448
github.com/go-logfmt/logfmt v0.6.0 h1:wGYYu3uicYdqXVgoYbvnkrPVXkuLM1p1ifugDMEdRi4=
@@ -99,6 +103,8 @@ github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWE
99103
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
100104
github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc=
101105
github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
106+
github.com/maypok86/otter v1.2.4 h1:HhW1Pq6VdJkmWwcZZq19BlEQkHtI8xgsQzBVXJU0nfc=
107+
github.com/maypok86/otter v1.2.4/go.mod h1:mKLfoI7v1HOmQMwFgX4QkRk23mX6ge3RDvjdHOWG4R4=
102108
github.com/mitchellh/go-testing-interface v1.14.1 h1:jrgshOhYAUVNMAJiKbEu7EqAwgJJ2JqpQmpLJOu07cU=
103109
github.com/mitchellh/go-testing-interface v1.14.1/go.mod h1:gfgS7OtZj6MA4U1UrDRp04twqAjfvlZyCfX3sDjEym8=
104110
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=

0 commit comments

Comments
 (0)