mirror of
https://github.com/aljazceru/lnd-manageJ.git
synced 2026-01-24 08:24:20 +01:00
cache peers/nodes
This commit is contained in:
@@ -10,13 +10,18 @@ import org.springframework.stereotype.Component;
|
||||
@Component
|
||||
public class NodeService {
|
||||
private static final int MAXIMUM_SIZE = 500;
|
||||
private static final int CACHE_EXPIRY_MINUTES = 30;
|
||||
private static final int ALIAS_CACHE_EXPIRY_MINUTES = 30;
|
||||
private static final int NODE_CACHE_EXPIRY_SECONDS = 60;
|
||||
|
||||
private final GrpcNodeInfo grpcNodeInfo;
|
||||
private final LoadingCache<Pubkey, String> aliasCache = new CacheBuilder()
|
||||
.withExpiryMinutes(CACHE_EXPIRY_MINUTES)
|
||||
.withExpiryMinutes(ALIAS_CACHE_EXPIRY_MINUTES)
|
||||
.withMaximumSize(MAXIMUM_SIZE)
|
||||
.build(this::getAliasWithoutCache);
|
||||
private final LoadingCache<Pubkey, Node> nodeCache = new CacheBuilder()
|
||||
.withExpirySeconds(NODE_CACHE_EXPIRY_SECONDS)
|
||||
.withMaximumSize(MAXIMUM_SIZE)
|
||||
.build(this::getNodeWithoutCache);
|
||||
|
||||
public NodeService(GrpcNodeInfo grpcNodeInfo) {
|
||||
this.grpcNodeInfo = grpcNodeInfo;
|
||||
@@ -27,11 +32,17 @@ public class NodeService {
|
||||
}
|
||||
|
||||
public Node getNode(Pubkey pubkey) {
|
||||
return grpcNodeInfo.getNode(pubkey);
|
||||
return nodeCache.getUnchecked(pubkey);
|
||||
}
|
||||
|
||||
private Node getNodeWithoutCache(Pubkey pubkey) {
|
||||
Node node = grpcNodeInfo.getNode(pubkey);
|
||||
aliasCache.put(pubkey, node.alias());
|
||||
return node;
|
||||
}
|
||||
|
||||
private String getAliasWithoutCache(Pubkey pubkey) {
|
||||
return getNode(pubkey).alias();
|
||||
return getNodeWithoutCache(pubkey).alias();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user