diff --git a/application/src/main/java/de/cotto/lndmanagej/service/ChannelService.java b/application/src/main/java/de/cotto/lndmanagej/service/ChannelService.java index 5c4b5d7b..06881d05 100644 --- a/application/src/main/java/de/cotto/lndmanagej/service/ChannelService.java +++ b/application/src/main/java/de/cotto/lndmanagej/service/ChannelService.java @@ -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> 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); } 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 7faaae91..fff4b53a 100644 --- a/application/src/main/java/de/cotto/lndmanagej/service/NodeService.java +++ b/application/src/main/java/de/cotto/lndmanagej/service/NodeService.java @@ -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 aliasCache = new CacheBuilder() - .withExpiryMinutes(ALIAS_CACHE_EXPIRY_MINUTES) + .withExpiry(ALIAS_CACHE_EXPIRY) .withMaximumSize(MAXIMUM_SIZE) .build(this::getAliasWithoutCache); private final LoadingCache nodeCache = new CacheBuilder() - .withExpirySeconds(NODE_CACHE_EXPIRY_SECONDS) + .withExpiry(NODE_CACHE_EXPIRY) .withMaximumSize(MAXIMUM_SIZE) .build(this::getNodeWithoutCache); diff --git a/application/src/main/java/de/cotto/lndmanagej/service/OwnNodeService.java b/application/src/main/java/de/cotto/lndmanagej/service/OwnNodeService.java index b7bb9449..85352f9c 100644 --- a/application/src/main/java/de/cotto/lndmanagej/service/OwnNodeService.java +++ b/application/src/main/java/de/cotto/lndmanagej/service/OwnNodeService.java @@ -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 syncedToChainCache = new CacheBuilder() - .withExpirySeconds(CACHE_EXPIRY_SECONDS) + .withExpiry(CACHE_EXPIRY) .build(this::isSyncedToChainWithoutCache); public OwnNodeService(GrpcGetInfo grpcGetInfo) { diff --git a/caching/src/main/java/de/cotto/lndmanagej/caching/CacheBuilder.java b/caching/src/main/java/de/cotto/lndmanagej/caching/CacheBuilder.java index 48ed1fe2..8d2a40d0 100644 --- a/caching/src/main/java/de/cotto/lndmanagej/caching/CacheBuilder.java +++ b/caching/src/main/java/de/cotto/lndmanagej/caching/CacheBuilder.java @@ -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 LoadingCache build(Function function) { CacheLoader loader = getLoader(function); com.google.common.cache.CacheBuilder builder = com.google.common.cache.CacheBuilder.newBuilder() - .expireAfterWrite(duration, Objects.requireNonNull(timeUnit)); + .expireAfterWrite(duration); if (this.maximumSize != null) { return builder .maximumSize(maximumSize) diff --git a/caching/src/test/java/de/cotto/lndmanagej/caching/CacheBuilderTest.java b/caching/src/test/java/de/cotto/lndmanagej/caching/CacheBuilderTest.java index 5c7f56a8..67030e06 100644 --- a/caching/src/test/java/de/cotto/lndmanagej/caching/CacheBuilderTest.java +++ b/caching/src/test/java/de/cotto/lndmanagej/caching/CacheBuilderTest.java @@ -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 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 cache = new CacheBuilder() - .withExpirySeconds(1) - .build(System::nanoTime); - assertIsCached(cache); - } - @Test void expiryOneMinute() { LoadingCache cache = new CacheBuilder() - .withExpiryMinutes(1) + .withExpiry(Duration.ofMinutes(1)) .build(System::nanoTime); assertIsCached(cache); } @@ -43,7 +37,7 @@ class CacheBuilderTest { @Test void withMaximumSize() { LoadingCache cache = new CacheBuilder() - .withExpiryMinutes(1) + .withExpiry(Duration.ofMinutes(1)) .withMaximumSize(1) .build(System::nanoTime); Long first = cache.getUnchecked(""); diff --git a/grpc-adapter/src/main/java/de/cotto/lndmanagej/grpc/GrpcService.java b/grpc-adapter/src/main/java/de/cotto/lndmanagej/grpc/GrpcService.java index e4061246..9cee29d2 100644 --- a/grpc-adapter/src/main/java/de/cotto/lndmanagej/grpc/GrpcService.java +++ b/grpc-adapter/src/main/java/de/cotto/lndmanagej/grpc/GrpcService.java @@ -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> channelsCache = new CacheBuilder() - .withExpiryMilliseconds(CHANNELS_CACHE_EXPIRY_MS) + .withExpiry(CHANNELS_CACHE_EXPIRY) .build(this::getChannelsWithoutCache); private final LoadingCache> pendingChannelsCache = new CacheBuilder() - .withExpiryMilliseconds(PENDING_CHANNELS_CACHE_EXPIRY_MS) + .withExpiry(PENDING_CHANNELS_CACHE_EXPIRY) .build(this::getPendingChannelsWithoutCache); private final LoadingCache> listPeersCache = new CacheBuilder() - .withExpiryMilliseconds(LIST_PEERS_CACHE_EXPIRY_MS) + .withExpiry(LIST_PEERS_CACHE_EXPIRY) .build(this::listPeersWithoutCache); private final LoadingCache>> getTransactionsCache = new CacheBuilder() - .withExpiryMilliseconds(TRANSACTIONS_CACHE_EXPIRY_MS) + .withExpiry(TRANSACTIONS_CACHE_EXPIRY) .build(this::getTransactionsWithoutCache); public GrpcService(LndConfiguration lndConfiguration, Metrics metrics) throws IOException { diff --git a/transactions/src/main/java/de/cotto/lndmanagej/transactions/service/TransactionService.java b/transactions/src/main/java/de/cotto/lndmanagej/transactions/service/TransactionService.java index 4c1ffd07..9a48bba5 100644 --- a/transactions/src/main/java/de/cotto/lndmanagej/transactions/service/TransactionService.java +++ b/transactions/src/main/java/de/cotto/lndmanagej/transactions/service/TransactionService.java @@ -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); }