move cache to service

This commit is contained in:
Carsten Otto
2021-11-12 09:29:46 +01:00
parent 70e7683fcb
commit 51f2a9ecca
3 changed files with 22 additions and 25 deletions

View File

@@ -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'

View File

@@ -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) {

View File

@@ -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();