mirror of
https://github.com/aljazceru/lnd-manageJ.git
synced 2026-01-25 17:04:22 +01:00
move cache to service
This commit is contained in:
@@ -5,6 +5,7 @@ plugins {
|
||||
dependencies {
|
||||
implementation('org.springframework.boot:spring-boot-starter-web')
|
||||
implementation('org.springframework.boot:spring-boot-starter-actuator')
|
||||
implementation('com.google.guava:guava:31.0.1-jre')
|
||||
implementation project(':grpc-adapter')
|
||||
implementation project(':model')
|
||||
runtimeOnly 'org.postgresql:postgresql'
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
package de.cotto.lndmanagej.service;
|
||||
|
||||
import com.google.common.cache.CacheBuilder;
|
||||
import com.google.common.cache.CacheLoader;
|
||||
import com.google.common.cache.LoadingCache;
|
||||
import de.cotto.lndmanagej.grpc.GrpcNodeInfo;
|
||||
import de.cotto.lndmanagej.model.Channel;
|
||||
import de.cotto.lndmanagej.model.ChannelId;
|
||||
@@ -7,17 +10,34 @@ import de.cotto.lndmanagej.model.Node;
|
||||
import de.cotto.lndmanagej.model.Pubkey;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Component
|
||||
public class NodeService {
|
||||
private static final int MAXIMUM_SIZE = 500;
|
||||
private static final int CACHE_EXPIRY_MINUTES = 30;
|
||||
|
||||
private final GrpcNodeInfo grpcNodeInfo;
|
||||
private final ChannelService channelService;
|
||||
private final LoadingCache<Pubkey, String> cache;
|
||||
|
||||
public NodeService(GrpcNodeInfo grpcNodeInfo, ChannelService channelService) {
|
||||
this.grpcNodeInfo = grpcNodeInfo;
|
||||
this.channelService = channelService;
|
||||
CacheLoader<Pubkey, String> loader = new CacheLoader<>() {
|
||||
@Nonnull
|
||||
@Override
|
||||
public String load(@Nonnull Pubkey pubkey) {
|
||||
return getNode(pubkey).alias();
|
||||
}
|
||||
};
|
||||
cache = CacheBuilder.newBuilder()
|
||||
.expireAfterWrite(CACHE_EXPIRY_MINUTES, TimeUnit.MINUTES)
|
||||
.maximumSize(MAXIMUM_SIZE)
|
||||
.build(loader);
|
||||
}
|
||||
|
||||
public List<ChannelId> getOpenChannelIds(Pubkey pubkey) {
|
||||
@@ -29,7 +49,7 @@ public class NodeService {
|
||||
}
|
||||
|
||||
public String getAlias(Pubkey pubkey) {
|
||||
return getNode(pubkey).alias();
|
||||
return cache.getUnchecked(pubkey);
|
||||
}
|
||||
|
||||
private Node getNode(Pubkey pubkey) {
|
||||
|
||||
@@ -1,44 +1,20 @@
|
||||
package de.cotto.lndmanagej.grpc;
|
||||
|
||||
import com.google.common.cache.CacheBuilder;
|
||||
import com.google.common.cache.CacheLoader;
|
||||
import com.google.common.cache.LoadingCache;
|
||||
import de.cotto.lndmanagej.model.Node;
|
||||
import de.cotto.lndmanagej.model.Pubkey;
|
||||
import lnrpc.LightningNode;
|
||||
import lnrpc.NodeInfo;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@Component
|
||||
public class GrpcNodeInfo {
|
||||
private static final int MAXIMUM_SIZE = 500;
|
||||
private static final int CACHE_EXPIRY_MINUTES = 30;
|
||||
|
||||
private final GrpcService grpcService;
|
||||
private final LoadingCache<Pubkey, Node> cache;
|
||||
|
||||
public GrpcNodeInfo(GrpcService grpcService) {
|
||||
this.grpcService = grpcService;
|
||||
CacheLoader<Pubkey, Node> loader = new CacheLoader<>() {
|
||||
@Override
|
||||
public Node load(@Nonnull Pubkey pubkey) {
|
||||
return getNodeWithoutCache(pubkey);
|
||||
}
|
||||
};
|
||||
cache = CacheBuilder.newBuilder()
|
||||
.expireAfterWrite(CACHE_EXPIRY_MINUTES, TimeUnit.MINUTES)
|
||||
.maximumSize(MAXIMUM_SIZE)
|
||||
.build(loader);
|
||||
}
|
||||
|
||||
public Node getNode(Pubkey pubkey) {
|
||||
return cache.getUnchecked(pubkey);
|
||||
}
|
||||
|
||||
private Node getNodeWithoutCache(Pubkey pubkey) {
|
||||
NodeInfo nodeInfo = grpcService.getNodeInfo(pubkey).orElse(null);
|
||||
if (nodeInfo == null) {
|
||||
return Node.builder().withPubkey(pubkey).build();
|
||||
|
||||
Reference in New Issue
Block a user