get closed channels

This commit is contained in:
Carsten Otto
2021-11-15 09:25:06 +01:00
parent 66a1f809d1
commit 9013d23feb
18 changed files with 287 additions and 110 deletions

View File

@@ -3,7 +3,8 @@ package de.cotto.lndmanagej.service;
import com.google.common.cache.LoadingCache;
import de.cotto.lndmanagej.caching.CacheBuilder;
import de.cotto.lndmanagej.grpc.GrpcChannels;
import de.cotto.lndmanagej.model.LocalChannel;
import de.cotto.lndmanagej.model.ClosedChannel;
import de.cotto.lndmanagej.model.LocalOpenChannel;
import de.cotto.lndmanagej.model.Pubkey;
import org.springframework.stereotype.Component;
@@ -12,26 +13,31 @@ import java.util.stream.Collectors;
@Component
public class ChannelService {
private static final int MAXIMUM_SIZE = 500;
private static final int CACHE_EXPIRY_MINUTES = 1;
private final LoadingCache<Object, Set<LocalChannel>> channelsCache;
private final LoadingCache<Object, Set<LocalOpenChannel>> channelsCache;
private final LoadingCache<Object, Set<ClosedChannel>> closedChannelsCache;
public ChannelService(GrpcChannels grpcChannels) {
channelsCache = new CacheBuilder()
.withExpiryMinutes(CACHE_EXPIRY_MINUTES)
.withMaximumSize(MAXIMUM_SIZE)
.build(grpcChannels::getChannels);
closedChannelsCache = new CacheBuilder()
.withExpiryMinutes(CACHE_EXPIRY_MINUTES)
.build(grpcChannels::getClosedChannels);
}
public Set<LocalChannel> getOpenChannels() {
public Set<LocalOpenChannel> getOpenChannels() {
return channelsCache.getUnchecked("");
}
public Set<LocalChannel> getOpenChannelsWith(Pubkey peer) {
public Set<ClosedChannel> getClosedChannels() {
return closedChannelsCache.getUnchecked("");
}
public Set<LocalOpenChannel> getOpenChannelsWith(Pubkey peer) {
return getOpenChannels().stream()
.filter(c -> peer.equals(c.getRemotePubkey()))
.collect(Collectors.toSet());
}
}