add cache for getChannels

This commit is contained in:
Carsten Otto
2021-11-12 22:25:06 +01:00
parent 9cd344b7ae
commit 0eefa8db0e
10 changed files with 177 additions and 61 deletions

View File

@@ -1,16 +1,13 @@
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.caching.CacheBuilder;
import de.cotto.lndmanagej.grpc.GrpcChannels;
import de.cotto.lndmanagej.model.LocalChannel;
import de.cotto.lndmanagej.model.Pubkey;
import org.springframework.stereotype.Component;
import javax.annotation.Nonnull;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
@Component
@@ -18,12 +15,13 @@ public class ChannelService {
private static final int MAXIMUM_SIZE = 500;
private static final int CACHE_EXPIRY_MINUTES = 1;
private final GrpcChannels grpcChannels;
private final LoadingCache<String, Set<LocalChannel>> channelsCache;
private final LoadingCache<Object, Set<LocalChannel>> channelsCache;
public ChannelService(GrpcChannels grpcChannels) {
this.grpcChannels = grpcChannels;
channelsCache = initializeChannelsCache();
channelsCache = new CacheBuilder()
.withExpiryMinutes(CACHE_EXPIRY_MINUTES)
.withMaximumSize(MAXIMUM_SIZE)
.build(grpcChannels::getChannels);
}
public Set<LocalChannel> getOpenChannels() {
@@ -36,17 +34,4 @@ public class ChannelService {
.collect(Collectors.toSet());
}
private LoadingCache<String, Set<LocalChannel>> initializeChannelsCache() {
CacheLoader<String, Set<LocalChannel>> loader = new CacheLoader<>() {
@Nonnull
@Override
public Set<LocalChannel> load(@Nonnull String ignored) {
return grpcChannels.getChannels();
}
};
return CacheBuilder.newBuilder()
.expireAfterWrite(CACHE_EXPIRY_MINUTES, TimeUnit.MINUTES)
.maximumSize(MAXIMUM_SIZE)
.build(loader);
}
}

View File

@@ -1,44 +1,32 @@
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.caching.CacheBuilder;
import de.cotto.lndmanagej.grpc.GrpcNodeInfo;
import de.cotto.lndmanagej.model.Node;
import de.cotto.lndmanagej.model.Pubkey;
import org.springframework.stereotype.Component;
import javax.annotation.Nonnull;
import java.util.concurrent.TimeUnit;
@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 LoadingCache<Pubkey, String> aliasCache;
private final LoadingCache<Pubkey, String> aliasCache = new CacheBuilder()
.withExpiryMinutes(CACHE_EXPIRY_MINUTES)
.withMaximumSize(MAXIMUM_SIZE)
.build(this::getAliasWithoutCache);
public NodeService(GrpcNodeInfo grpcNodeInfo) {
this.grpcNodeInfo = grpcNodeInfo;
CacheLoader<Pubkey, String> loader = new CacheLoader<>() {
@Nonnull
@Override
public String load(@Nonnull Pubkey pubkey) {
return getNode(pubkey).alias();
}
};
aliasCache = CacheBuilder.newBuilder()
.expireAfterWrite(CACHE_EXPIRY_MINUTES, TimeUnit.MINUTES)
.maximumSize(MAXIMUM_SIZE)
.build(loader);
}
public String getAlias(Pubkey pubkey) {
return aliasCache.getUnchecked(pubkey);
}
private Node getNode(Pubkey pubkey) {
return grpcNodeInfo.getNode(pubkey);
private String getAliasWithoutCache(Pubkey pubkey) {
return grpcNodeInfo.getNode(pubkey).alias();
}
}

View File

@@ -1,34 +1,21 @@
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.caching.CacheBuilder;
import de.cotto.lndmanagej.grpc.GrpcGetInfo;
import org.springframework.stereotype.Component;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.concurrent.TimeUnit;
@Component
public class OwnNodeService {
private static final int CACHE_EXPIRY_SECONDS = 30;
private final GrpcGetInfo grpcGetInfo;
private final LoadingCache<String, Boolean> syncedToChainCache;
private final LoadingCache<Object, Boolean> syncedToChainCache = new CacheBuilder()
.withExpirySeconds(CACHE_EXPIRY_SECONDS)
.build(this::isSyncedToChainWithoutCache);
public OwnNodeService(GrpcGetInfo grpcGetInfo) {
this.grpcGetInfo = grpcGetInfo;
CacheLoader<String, Boolean> loader = new CacheLoader<>() {
@Nonnull
@Override
public Boolean load(@Nullable String ignored) {
return isSyncedToChainWithoutCache();
}
};
syncedToChainCache = CacheBuilder.newBuilder()
.expireAfterWrite(CACHE_EXPIRY_SECONDS, TimeUnit.SECONDS)
.build(loader);
}
public boolean isSyncedToChain() {