Skip to content

Commit 1b6bba4

Browse files
committed
graphdb: check for cached data before query
In this commit, we first check for the node in our cache before querying the database when determining if a node is public or not.
1 parent 2d51667 commit 1b6bba4

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

graph/db/sql_store.go

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/btcsuite/btcd/btcutil"
2121
"github.com/btcsuite/btcd/chaincfg/chainhash"
2222
"github.com/btcsuite/btcd/wire"
23+
"github.com/lightninglabs/neutrino/cache"
2324
"github.com/lightninglabs/neutrino/cache/lru"
2425
"github.com/lightningnetwork/lnd/aliasmgr"
2526
"github.com/lightningnetwork/lnd/batch"
@@ -2299,8 +2300,28 @@ func (s *SQLStore) ChannelID(chanPoint *wire.OutPoint) (uint64, error) {
22992300
func (s *SQLStore) IsPublicNode(pubKey [33]byte) (bool, error) {
23002301
ctx := context.TODO()
23012302

2303+
// Check the cache first with a read lock.
2304+
s.cacheMu.RLock()
2305+
cached, err := s.publicNodeCache.Get(pubKey)
2306+
2307+
switch {
2308+
case errors.Is(err, cache.ErrElementNotFound):
2309+
// Cache not found, so we'll need to fetch the node from the
2310+
// database.
2311+
2312+
case cached != nil:
2313+
s.cacheMu.RUnlock()
2314+
return cached.isPublic, nil
2315+
2316+
case err != nil:
2317+
s.cacheMu.RUnlock()
2318+
log.Warnf("unable to check cache if node is public: %w", err)
2319+
}
2320+
2321+
s.cacheMu.RUnlock()
2322+
23022323
var isPublic bool
2303-
err := s.db.ExecTx(ctx, sqldb.ReadTxOpt(), func(db SQLQueries) error {
2324+
err = s.db.ExecTx(ctx, sqldb.ReadTxOpt(), func(db SQLQueries) error {
23042325
var err error
23052326
isPublic, err = db.IsPublicV1Node(ctx, pubKey[:])
23062327

@@ -2311,6 +2332,17 @@ func (s *SQLStore) IsPublicNode(pubKey [33]byte) (bool, error) {
23112332
"public: %w", err)
23122333
}
23132334

2335+
// Store the result in cache.
2336+
s.cacheMu.Lock()
2337+
_, err = s.publicNodeCache.Put(pubKey, &cachedPublicNode{
2338+
isPublic: isPublic,
2339+
})
2340+
if err != nil {
2341+
log.Warnf("unable to store node info in cache: %w", err)
2342+
}
2343+
2344+
s.cacheMu.Unlock()
2345+
23142346
return isPublic, nil
23152347
}
23162348

0 commit comments

Comments
 (0)