Skip to content

Commit b9214d0

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 b3c649e commit b9214d0

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

graph/db/sql_store.go

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/btcsuite/btcd/btcutil"
2222
"github.com/btcsuite/btcd/chaincfg/chainhash"
2323
"github.com/btcsuite/btcd/wire"
24+
"github.com/lightninglabs/neutrino/cache"
2425
"github.com/lightninglabs/neutrino/cache/lru"
2526
"github.com/lightningnetwork/lnd/aliasmgr"
2627
"github.com/lightningnetwork/lnd/batch"
@@ -2310,8 +2311,27 @@ func (s *SQLStore) ChannelID(chanPoint *wire.OutPoint) (uint64, error) {
23102311
func (s *SQLStore) IsPublicNode(pubKey [33]byte) (bool, error) {
23112312
ctx := context.TODO()
23122313

2314+
// Check the cache first with a read lock.
2315+
s.cacheMu.RLock()
2316+
cached, err := s.publicNodeCache.Get(pubKey)
2317+
2318+
switch {
2319+
case errors.Is(err, cache.ErrElementNotFound):
2320+
// Cache not found, so we'll need to fetch the node from the
2321+
// database.
2322+
2323+
case cached != nil:
2324+
s.cacheMu.RUnlock()
2325+
return cached.isPublic, nil
2326+
2327+
case err != nil:
2328+
log.Warnf("unable to check cache if node is public: %w", err)
2329+
}
2330+
2331+
s.cacheMu.RUnlock()
2332+
23132333
var isPublic bool
2314-
err := s.db.ExecTx(ctx, sqldb.ReadTxOpt(), func(db SQLQueries) error {
2334+
err = s.db.ExecTx(ctx, sqldb.ReadTxOpt(), func(db SQLQueries) error {
23152335
var err error
23162336
isPublic, err = db.IsPublicV1Node(ctx, pubKey[:])
23172337

@@ -2322,6 +2342,17 @@ func (s *SQLStore) IsPublicNode(pubKey [33]byte) (bool, error) {
23222342
"public: %w", err)
23232343
}
23242344

2345+
// Store the result in cache.
2346+
s.cacheMu.Lock()
2347+
_, err = s.publicNodeCache.Put(pubKey, &cachedPublicNode{
2348+
isPublic: isPublic,
2349+
})
2350+
if err != nil {
2351+
log.Warnf("unable to store node info in cache: %w", err)
2352+
}
2353+
2354+
s.cacheMu.Unlock()
2355+
23252356
return isPublic, nil
23262357
}
23272358

0 commit comments

Comments
 (0)