From 51f2a9ecca11eea7dfdac295266eec8fc6d92999 Mon Sep 17 00:00:00 2001 From: Carsten Otto Date: Fri, 12 Nov 2021 09:29:46 +0100 Subject: [PATCH] move cache to service --- application/build.gradle | 1 + .../cotto/lndmanagej/service/NodeService.java | 22 ++++++++++++++++- .../cotto/lndmanagej/grpc/GrpcNodeInfo.java | 24 ------------------- 3 files changed, 22 insertions(+), 25 deletions(-) diff --git a/application/build.gradle b/application/build.gradle index ae447963..ce48c3f1 100644 --- a/application/build.gradle +++ b/application/build.gradle @@ -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' diff --git a/application/src/main/java/de/cotto/lndmanagej/service/NodeService.java b/application/src/main/java/de/cotto/lndmanagej/service/NodeService.java index 4a7c5eef..812f8900 100644 --- a/application/src/main/java/de/cotto/lndmanagej/service/NodeService.java +++ b/application/src/main/java/de/cotto/lndmanagej/service/NodeService.java @@ -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 cache; public NodeService(GrpcNodeInfo grpcNodeInfo, ChannelService channelService) { this.grpcNodeInfo = grpcNodeInfo; this.channelService = channelService; + CacheLoader 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 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) { diff --git a/grpc-adapter/src/main/java/de/cotto/lndmanagej/grpc/GrpcNodeInfo.java b/grpc-adapter/src/main/java/de/cotto/lndmanagej/grpc/GrpcNodeInfo.java index 66ea64d8..9c1faf4f 100644 --- a/grpc-adapter/src/main/java/de/cotto/lndmanagej/grpc/GrpcNodeInfo.java +++ b/grpc-adapter/src/main/java/de/cotto/lndmanagej/grpc/GrpcNodeInfo.java @@ -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 cache; public GrpcNodeInfo(GrpcService grpcService) { this.grpcService = grpcService; - CacheLoader 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();