From a64e9052dd0b6ad294377c238a74f403713b2f6e Mon Sep 17 00:00:00 2001 From: kennycud Date: Wed, 4 Sep 2024 16:24:08 -0700 Subject: [PATCH] consolidated the cache limits into an attribute in Settings.java --- .../qortal/crosschain/BlockchainCache.java | 22 +++++++++---------- .../java/org/qortal/settings/Settings.java | 6 +++++ 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/main/java/org/qortal/crosschain/BlockchainCache.java b/src/main/java/org/qortal/crosschain/BlockchainCache.java index bfc6f3c6..f6a1acf6 100644 --- a/src/main/java/org/qortal/crosschain/BlockchainCache.java +++ b/src/main/java/org/qortal/crosschain/BlockchainCache.java @@ -1,6 +1,7 @@ package org.qortal.crosschain; import org.bitcoinj.crypto.DeterministicKey; +import org.qortal.settings.Settings; import java.util.Optional; import java.util.Queue; @@ -29,18 +30,11 @@ public class BlockchainCache { private ConcurrentHashMap transactionByHash = new ConcurrentHashMap<>(); /** - * Key History Limit + * Cache Limit * - * If this limit is reached, the cache will be reduced. + * If this limit is reached, the cache will be cleared or reduced. */ - private static final int KEY_HISTORY_LIMIT = 10000; - - /** - * Transaction Limit - * - * If this limit is reached, the cache will be cleared. - */ - private static final int TRANSACTION_LIMIT = 10000; + private static final int CACHE_LIMIT = Settings.getInstance().getBlockchainCacheLimit(); /** * Add Key With History @@ -49,7 +43,9 @@ public class BlockchainCache { */ public void addKeyWithHistory(DeterministicKey key) { - if( this.keysWithHistory.size() > KEY_HISTORY_LIMIT ) this.keysWithHistory.remove(); + if( this.keysWithHistory.size() > CACHE_LIMIT ) { + this.keysWithHistory.remove(); + } this.keysWithHistory.add(key); } @@ -73,7 +69,9 @@ public class BlockchainCache { */ public void addTransactionByHash( String hash, BitcoinyTransaction transaction ) { - if( this.transactionByHash.size() > TRANSACTION_LIMIT ) this.transactionByHash.clear(); + if( this.transactionByHash.size() > CACHE_LIMIT ) { + this.transactionByHash.clear(); + } this.transactionByHash.put(hash, transaction); } diff --git a/src/main/java/org/qortal/settings/Settings.java b/src/main/java/org/qortal/settings/Settings.java index 90dab19b..f18ccd88 100644 --- a/src/main/java/org/qortal/settings/Settings.java +++ b/src/main/java/org/qortal/settings/Settings.java @@ -328,6 +328,9 @@ public class Settings { /** How many wallet keys to generate when using bitcoinj as the blockchain interface (e.g. when sending coins) */ private int bitcoinjLookaheadSize = 50; + /** How many units of data to be kept in a blockchain cache before the cache should be reduced or cleared. */ + private int blockchainCacheLimit = 1000; + // Data storage (QDN) /** Data storage enabled/disabled*/ @@ -1049,6 +1052,9 @@ public class Settings { return bitcoinjLookaheadSize; } + public int getBlockchainCacheLimit() { + return blockchainCacheLimit; + } public boolean isQdnEnabled() { return this.qdnEnabled;