mirror of
https://github.com/aljazceru/lnd-manageJ.git
synced 2026-01-09 09:14:28 +01:00
migrate to duration as expiry parameter
This commit is contained in:
@@ -13,6 +13,7 @@ import de.cotto.lndmanagej.model.Pubkey;
|
||||
import de.cotto.lndmanagej.model.WaitingCloseChannel;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.Collection;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
@@ -22,7 +23,7 @@ import java.util.stream.Stream;
|
||||
|
||||
@Component
|
||||
public class ChannelService {
|
||||
private static final int CACHE_EXPIRY_MINUTES = 1;
|
||||
private static final Duration CACHE_EXPIRY = Duration.ofMinutes(1);
|
||||
|
||||
private final GrpcChannels grpcChannels;
|
||||
private final LoadingCache<Object, Set<LocalOpenChannel>> channelsCache;
|
||||
@@ -33,16 +34,16 @@ public class ChannelService {
|
||||
public ChannelService(GrpcChannels grpcChannels, GrpcClosedChannels grpcClosedChannels) {
|
||||
this.grpcChannels = grpcChannels;
|
||||
channelsCache = new CacheBuilder()
|
||||
.withExpiryMinutes(CACHE_EXPIRY_MINUTES)
|
||||
.withExpiry(CACHE_EXPIRY)
|
||||
.build(grpcChannels::getChannels);
|
||||
closedChannelsCache = new CacheBuilder()
|
||||
.withExpiryMinutes(CACHE_EXPIRY_MINUTES)
|
||||
.withExpiry(CACHE_EXPIRY)
|
||||
.build(grpcClosedChannels::getClosedChannels);
|
||||
forceClosingChannelsCache = new CacheBuilder()
|
||||
.withExpiryMinutes(CACHE_EXPIRY_MINUTES)
|
||||
.withExpiry(CACHE_EXPIRY)
|
||||
.build(grpcChannels::getForceClosingChannels);
|
||||
waitingCloseChannelsCache = new CacheBuilder()
|
||||
.withExpiryMinutes(CACHE_EXPIRY_MINUTES)
|
||||
.withExpiry(CACHE_EXPIRY)
|
||||
.build(grpcChannels::getWaitingCloseChannels);
|
||||
}
|
||||
|
||||
|
||||
@@ -7,19 +7,21 @@ import de.cotto.lndmanagej.model.Node;
|
||||
import de.cotto.lndmanagej.model.Pubkey;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
@Component
|
||||
public class NodeService {
|
||||
private static final int MAXIMUM_SIZE = 500;
|
||||
private static final int ALIAS_CACHE_EXPIRY_MINUTES = 30;
|
||||
private static final int NODE_CACHE_EXPIRY_SECONDS = 60;
|
||||
private static final Duration ALIAS_CACHE_EXPIRY = Duration.ofMinutes(30);
|
||||
private static final Duration NODE_CACHE_EXPIRY = Duration.ofSeconds(60);
|
||||
|
||||
private final GrpcNodeInfo grpcNodeInfo;
|
||||
private final LoadingCache<Pubkey, String> aliasCache = new CacheBuilder()
|
||||
.withExpiryMinutes(ALIAS_CACHE_EXPIRY_MINUTES)
|
||||
.withExpiry(ALIAS_CACHE_EXPIRY)
|
||||
.withMaximumSize(MAXIMUM_SIZE)
|
||||
.build(this::getAliasWithoutCache);
|
||||
private final LoadingCache<Pubkey, Node> nodeCache = new CacheBuilder()
|
||||
.withExpirySeconds(NODE_CACHE_EXPIRY_SECONDS)
|
||||
.withExpiry(NODE_CACHE_EXPIRY)
|
||||
.withMaximumSize(MAXIMUM_SIZE)
|
||||
.build(this::getNodeWithoutCache);
|
||||
|
||||
|
||||
@@ -5,13 +5,15 @@ import de.cotto.lndmanagej.caching.CacheBuilder;
|
||||
import de.cotto.lndmanagej.grpc.GrpcGetInfo;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
@Component
|
||||
public class OwnNodeService {
|
||||
private static final int CACHE_EXPIRY_SECONDS = 30;
|
||||
private static final Duration CACHE_EXPIRY = Duration.ofSeconds(30);
|
||||
|
||||
private final GrpcGetInfo grpcGetInfo;
|
||||
private final LoadingCache<Object, Boolean> syncedToChainCache = new CacheBuilder()
|
||||
.withExpirySeconds(CACHE_EXPIRY_SECONDS)
|
||||
.withExpiry(CACHE_EXPIRY)
|
||||
.build(this::isSyncedToChainWithoutCache);
|
||||
|
||||
public OwnNodeService(GrpcGetInfo grpcGetInfo) {
|
||||
|
||||
@@ -5,40 +5,22 @@ import com.google.common.cache.LoadingCache;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.time.Duration;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class CacheBuilder {
|
||||
private long duration;
|
||||
|
||||
@Nullable
|
||||
private TimeUnit timeUnit;
|
||||
private Duration duration;
|
||||
|
||||
@Nullable
|
||||
private Integer maximumSize;
|
||||
|
||||
public CacheBuilder() {
|
||||
duration = 10;
|
||||
timeUnit = TimeUnit.MINUTES;
|
||||
duration = Duration.ofMinutes(10);
|
||||
}
|
||||
|
||||
public CacheBuilder withExpiryMilliseconds(long milliseconds) {
|
||||
timeUnit = TimeUnit.MILLISECONDS;
|
||||
duration = milliseconds;
|
||||
return this;
|
||||
}
|
||||
|
||||
public CacheBuilder withExpirySeconds(long seconds) {
|
||||
timeUnit = TimeUnit.SECONDS;
|
||||
duration = seconds;
|
||||
return this;
|
||||
}
|
||||
|
||||
public CacheBuilder withExpiryMinutes(long minutes) {
|
||||
timeUnit = TimeUnit.MINUTES;
|
||||
duration = minutes;
|
||||
public CacheBuilder withExpiry(Duration duration) {
|
||||
this.duration = duration;
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -50,7 +32,7 @@ public class CacheBuilder {
|
||||
public <I, O> LoadingCache<I, O> build(Function<I, O> function) {
|
||||
CacheLoader<I, O> loader = getLoader(function);
|
||||
com.google.common.cache.CacheBuilder<Object, Object> builder = com.google.common.cache.CacheBuilder.newBuilder()
|
||||
.expireAfterWrite(duration, Objects.requireNonNull(timeUnit));
|
||||
.expireAfterWrite(duration);
|
||||
if (this.maximumSize != null) {
|
||||
return builder
|
||||
.maximumSize(maximumSize)
|
||||
|
||||
@@ -3,6 +3,8 @@ package de.cotto.lndmanagej.caching;
|
||||
import com.google.common.cache.LoadingCache;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class CacheBuilderTest {
|
||||
@@ -16,7 +18,7 @@ class CacheBuilderTest {
|
||||
@Test
|
||||
void expiryOneMillisecond() throws InterruptedException {
|
||||
LoadingCache<Object, Long> cache = new CacheBuilder()
|
||||
.withExpiryMilliseconds(1)
|
||||
.withExpiry(Duration.ofMillis(1))
|
||||
.build(System::nanoTime);
|
||||
Long first = cache.getUnchecked("");
|
||||
Thread.sleep(1);
|
||||
@@ -24,18 +26,10 @@ class CacheBuilderTest {
|
||||
assertThat(first).isNotEqualTo(second);
|
||||
}
|
||||
|
||||
@Test
|
||||
void expiryOneSecond() {
|
||||
LoadingCache<Object, Long> cache = new CacheBuilder()
|
||||
.withExpirySeconds(1)
|
||||
.build(System::nanoTime);
|
||||
assertIsCached(cache);
|
||||
}
|
||||
|
||||
@Test
|
||||
void expiryOneMinute() {
|
||||
LoadingCache<Object, Long> cache = new CacheBuilder()
|
||||
.withExpiryMinutes(1)
|
||||
.withExpiry(Duration.ofMinutes(1))
|
||||
.build(System::nanoTime);
|
||||
assertIsCached(cache);
|
||||
}
|
||||
@@ -43,7 +37,7 @@ class CacheBuilderTest {
|
||||
@Test
|
||||
void withMaximumSize() {
|
||||
LoadingCache<Object, Long> cache = new CacheBuilder()
|
||||
.withExpiryMinutes(1)
|
||||
.withExpiry(Duration.ofMinutes(1))
|
||||
.withMaximumSize(1)
|
||||
.build(System::nanoTime);
|
||||
Long first = cache.getUnchecked("");
|
||||
|
||||
@@ -29,29 +29,30 @@ import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.PreDestroy;
|
||||
import java.io.IOException;
|
||||
import java.time.Duration;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Component
|
||||
@SuppressWarnings("PMD.ExcessiveImports")
|
||||
public class GrpcService extends GrpcBase {
|
||||
private static final int CHANNELS_CACHE_EXPIRY_MS = 200;
|
||||
private static final int PENDING_CHANNELS_CACHE_EXPIRY_MS = 10_000;
|
||||
private static final int LIST_PEERS_CACHE_EXPIRY_MS = 10_000;
|
||||
private static final int TRANSACTIONS_CACHE_EXPIRY_MS = 30_000;
|
||||
private static final Duration CHANNELS_CACHE_EXPIRY = Duration.ofMillis(200);
|
||||
private static final Duration PENDING_CHANNELS_CACHE_EXPIRY = Duration.ofSeconds(10);
|
||||
private static final Duration LIST_PEERS_CACHE_EXPIRY = Duration.ofSeconds(10);
|
||||
private static final Duration TRANSACTIONS_CACHE_EXPIRY = Duration.ofSeconds(30);
|
||||
|
||||
private final LightningGrpc.LightningBlockingStub lightningStub;
|
||||
private final LoadingCache<Object, List<Channel>> channelsCache = new CacheBuilder()
|
||||
.withExpiryMilliseconds(CHANNELS_CACHE_EXPIRY_MS)
|
||||
.withExpiry(CHANNELS_CACHE_EXPIRY)
|
||||
.build(this::getChannelsWithoutCache);
|
||||
private final LoadingCache<Object, Optional<PendingChannelsResponse>> pendingChannelsCache = new CacheBuilder()
|
||||
.withExpiryMilliseconds(PENDING_CHANNELS_CACHE_EXPIRY_MS)
|
||||
.withExpiry(PENDING_CHANNELS_CACHE_EXPIRY)
|
||||
.build(this::getPendingChannelsWithoutCache);
|
||||
private final LoadingCache<Object, List<Peer>> listPeersCache = new CacheBuilder()
|
||||
.withExpiryMilliseconds(LIST_PEERS_CACHE_EXPIRY_MS)
|
||||
.withExpiry(LIST_PEERS_CACHE_EXPIRY)
|
||||
.build(this::listPeersWithoutCache);
|
||||
private final LoadingCache<Object, Optional<List<Transaction>>> getTransactionsCache = new CacheBuilder()
|
||||
.withExpiryMilliseconds(TRANSACTIONS_CACHE_EXPIRY_MS)
|
||||
.withExpiry(TRANSACTIONS_CACHE_EXPIRY)
|
||||
.build(this::getTransactionsWithoutCache);
|
||||
|
||||
public GrpcService(LndConfiguration lndConfiguration, Metrics metrics) throws IOException {
|
||||
|
||||
@@ -8,11 +8,13 @@ import de.cotto.lndmanagej.transactions.download.TransactionProvider;
|
||||
import de.cotto.lndmanagej.transactions.model.Transaction;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
@Component
|
||||
public class TransactionService {
|
||||
private static final Duration CACHE_EXPIRY = Duration.ofMinutes(1);
|
||||
private final TransactionDao transactionDao;
|
||||
private final TransactionProvider transactionProvider;
|
||||
private final GrpcTransactions grpcTransactions;
|
||||
@@ -27,7 +29,7 @@ public class TransactionService {
|
||||
this.transactionProvider = transactionProvider;
|
||||
this.grpcTransactions = grpcTransactions;
|
||||
hashIsKnownCache = new CacheBuilder()
|
||||
.withExpiryMinutes(1)
|
||||
.withExpiry(CACHE_EXPIRY)
|
||||
.withMaximumSize(1_000)
|
||||
.build(this::isKnownByLndWithoutCache);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user