move method to channelservice, add cache

This commit is contained in:
Carsten Otto
2021-11-12 09:51:41 +01:00
parent 3459f2ad8d
commit 13190eba9c
7 changed files with 58 additions and 47 deletions

View File

@@ -2,6 +2,7 @@ package de.cotto.lndmanagej.controller;
import de.cotto.lndmanagej.model.ChannelId;
import de.cotto.lndmanagej.model.Pubkey;
import de.cotto.lndmanagej.service.ChannelService;
import de.cotto.lndmanagej.service.NodeService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@@ -15,9 +16,11 @@ import java.util.stream.Collectors;
public class LegacyController {
private static final String NEWLINE = "\n";
private final NodeService nodeService;
private final ChannelService channelService;
public LegacyController(NodeService nodeService) {
public LegacyController(NodeService nodeService, ChannelService channelService) {
this.nodeService = nodeService;
this.channelService = channelService;
}
@GetMapping("/alias")
@@ -27,7 +30,7 @@ public class LegacyController {
@GetMapping("/open-channels")
public String getOpenChannelIds(@PathVariable Pubkey pubkey) {
return nodeService.getOpenChannelIds(pubkey).stream()
return channelService.getOpenChannelsWith(pubkey).stream()
.map(ChannelId::toString)
.collect(Collectors.joining(NEWLINE));
}

View File

@@ -1,30 +1,57 @@
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.GrpcChannels;
import de.cotto.lndmanagej.model.Channel;
import de.cotto.lndmanagej.model.ChannelId;
import de.cotto.lndmanagej.model.Node;
import de.cotto.lndmanagej.model.Pubkey;
import org.springframework.stereotype.Component;
import java.util.Set;
import javax.annotation.Nonnull;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
@Component
public class ChannelService {
private static final int MAXIMUM_SIZE = 500;
private static final int CACHE_EXPIRY_MINUTES = 5;
private final GrpcChannels grpcChannels;
private final LoadingCache<Node, List<ChannelId>> channelsWithPeerCache;
public ChannelService(GrpcChannels grpcChannels) {
this.grpcChannels = grpcChannels;
CacheLoader<Node, List<ChannelId>> loader = new CacheLoader<>() {
@Nonnull
@Override
public List<ChannelId> load(@Nonnull Node peer) {
return getOpenChannelsWithWithoutCache(peer);
}
};
channelsWithPeerCache = CacheBuilder.newBuilder()
.expireAfterWrite(CACHE_EXPIRY_MINUTES, TimeUnit.MINUTES)
.maximumSize(MAXIMUM_SIZE)
.build(loader);
}
public Set<Channel> getOpenChannelsWith(Pubkey peer) {
public List<ChannelId> getOpenChannelsWith(Pubkey peer) {
Node peerNode = Node.forPubkey(peer);
return getOpenChannelsWith(peerNode);
}
public Set<Channel> getOpenChannelsWith(Node peer) {
public List<ChannelId> getOpenChannelsWith(Node peer) {
return channelsWithPeerCache.getUnchecked(peer);
}
public List<ChannelId> getOpenChannelsWithWithoutCache(Node peer) {
return grpcChannels.getChannels().stream()
.filter(c -> c.getNodes().contains(peer))
.collect(Collectors.toSet());
.map(Channel::getId)
.sorted()
.collect(Collectors.toList());
}
}

View File

@@ -4,16 +4,12 @@ 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;
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 {
@@ -21,12 +17,10 @@ public class NodeService {
private static final int CACHE_EXPIRY_MINUTES = 30;
private final GrpcNodeInfo grpcNodeInfo;
private final ChannelService channelService;
private final LoadingCache<Pubkey, String> aliasCache;
public NodeService(GrpcNodeInfo grpcNodeInfo, ChannelService channelService) {
public NodeService(GrpcNodeInfo grpcNodeInfo) {
this.grpcNodeInfo = grpcNodeInfo;
this.channelService = channelService;
CacheLoader<Pubkey, String> loader = new CacheLoader<>() {
@Nonnull
@Override
@@ -40,13 +34,6 @@ public class NodeService {
.build(loader);
}
public List<ChannelId> getOpenChannelIds(Pubkey peer) {
return channelService.getOpenChannelsWith(peer).stream()
.map(Channel::getId)
.sorted()
.collect(Collectors.toList());
}
public String getAlias(Pubkey pubkey) {
return aliasCache.getUnchecked(pubkey);
}