3
0
mirror of https://github.com/Qortal/qortal.git synced 2025-02-12 02:05:50 +00:00

arbitraryDataCachedResources is now keyed by all ArbitraryDataResource elements, not just the resourceId

This commit is contained in:
CalDescent 2021-12-24 12:12:16 +00:00
parent e1e44d35bb
commit 42aca2e40f
2 changed files with 31 additions and 18 deletions

View File

@ -87,7 +87,7 @@ public class ArbitraryDataCache {
if (this.shouldInvalidateDueToSignatureMismatch()) { if (this.shouldInvalidateDueToSignatureMismatch()) {
// Add to the in-memory cache first, so that we won't check again for a while // Add to the in-memory cache first, so that we won't check again for a while
ArbitraryDataManager.getInstance().addResourceToCache(this.resourceId); ArbitraryDataManager.getInstance().addResourceToCache(this.getArbitraryDataResource());
return true; return true;
} }
@ -108,7 +108,7 @@ public class ArbitraryDataCache {
* @return whether to avoid lookups for this resource due to the in-memory cache * @return whether to avoid lookups for this resource due to the in-memory cache
*/ */
private boolean rateLimitInEffect() { private boolean rateLimitInEffect() {
return ArbitraryDataManager.getInstance().isResourceCached(this.resourceId); return ArbitraryDataManager.getInstance().isResourceCached(this.getArbitraryDataResource());
} }
private boolean shouldInvalidateDueToSignatureMismatch() { private boolean shouldInvalidateDueToSignatureMismatch() {
@ -158,4 +158,9 @@ public class ArbitraryDataCache {
} }
} }
private ArbitraryDataResource getArbitraryDataResource() {
// TODO: pass an ArbitraryDataResource into the constructor, rather than individual components
return new ArbitraryDataResource(this.resourceId, this.resourceIdType, this.service, this.identifier);
}
} }

View File

@ -5,6 +5,9 @@ import java.util.*;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.qortal.api.resource.TransactionsResource.ConfirmationStatus; import org.qortal.api.resource.TransactionsResource.ConfirmationStatus;
import org.qortal.arbitrary.ArbitraryDataFile;
import org.qortal.arbitrary.ArbitraryDataResource;
import org.qortal.arbitrary.misc.Service;
import org.qortal.controller.Controller; import org.qortal.controller.Controller;
import org.qortal.data.network.ArbitraryPeerData; import org.qortal.data.network.ArbitraryPeerData;
import org.qortal.data.transaction.ArbitraryTransactionData; import org.qortal.data.transaction.ArbitraryTransactionData;
@ -265,20 +268,20 @@ public class ArbitraryDataManager extends Thread {
ArbitraryDataFileManager.getInstance().cleanupRequestCache(now); ArbitraryDataFileManager.getInstance().cleanupRequestCache(now);
} }
public boolean isResourceCached(String resourceId) { public boolean isResourceCached(ArbitraryDataResource resource) {
if (resourceId == null) { if (resource == null) {
return false; return false;
} }
resourceId = resourceId.toLowerCase(); String key = resource.getUniqueKey();
// We don't have an entry for this resource ID, it is not cached // We don't have an entry for this resource ID, it is not cached
if (this.arbitraryDataCachedResources == null) { if (this.arbitraryDataCachedResources == null) {
return false; return false;
} }
if (!this.arbitraryDataCachedResources.containsKey(resourceId)) { if (!this.arbitraryDataCachedResources.containsKey(key)) {
return false; return false;
} }
Long timestamp = this.arbitraryDataCachedResources.get(resourceId); Long timestamp = this.arbitraryDataCachedResources.get(key);
if (timestamp == null) { if (timestamp == null) {
return false; return false;
} }
@ -286,7 +289,7 @@ public class ArbitraryDataManager extends Thread {
// If the timestamp has reached the timeout, we should remove it from the cache // If the timestamp has reached the timeout, we should remove it from the cache
long now = NTP.getTime(); long now = NTP.getTime();
if (now > timestamp) { if (now > timestamp) {
this.arbitraryDataCachedResources.remove(resourceId); this.arbitraryDataCachedResources.remove(key);
return false; return false;
} }
@ -294,11 +297,11 @@ public class ArbitraryDataManager extends Thread {
return true; return true;
} }
public void addResourceToCache(String resourceId) { public void addResourceToCache(ArbitraryDataResource resource) {
if (resourceId == null) { if (resource == null) {
return; return;
} }
resourceId = resourceId.toLowerCase(); String key = resource.getUniqueKey();
// Just in case // Just in case
if (this.arbitraryDataCachedResources == null) { if (this.arbitraryDataCachedResources == null) {
@ -312,7 +315,7 @@ public class ArbitraryDataManager extends Thread {
// Set the timestamp to now + the timeout // Set the timestamp to now + the timeout
Long timestamp = NTP.getTime() + ARBITRARY_DATA_CACHE_TIMEOUT; Long timestamp = NTP.getTime() + ARBITRARY_DATA_CACHE_TIMEOUT;
this.arbitraryDataCachedResources.put(resourceId, timestamp); this.arbitraryDataCachedResources.put(key, timestamp);
} }
public void invalidateCache(ArbitraryTransactionData arbitraryTransactionData) { public void invalidateCache(ArbitraryTransactionData arbitraryTransactionData) {
@ -320,17 +323,22 @@ public class ArbitraryDataManager extends Thread {
if (arbitraryTransactionData.getName() != null) { if (arbitraryTransactionData.getName() != null) {
String resourceId = arbitraryTransactionData.getName().toLowerCase(); String resourceId = arbitraryTransactionData.getName().toLowerCase();
LOGGER.info("We have all data for transaction {}", signature58); Service service = arbitraryTransactionData.getService();
LOGGER.info("Clearing cache for name {}...", arbitraryTransactionData.getName()); String identifier = arbitraryTransactionData.getIdentifier();
if (this.arbitraryDataCachedResources.containsKey(resourceId)) { ArbitraryDataResource resource =
this.arbitraryDataCachedResources.remove(resourceId); new ArbitraryDataResource(resourceId, ArbitraryDataFile.ResourceIdType.NAME, service, identifier);
String key = resource.getUniqueKey();
LOGGER.info("Clearing cache for {}...", resource);
if (this.arbitraryDataCachedResources.containsKey(key)) {
this.arbitraryDataCachedResources.remove(key);
} }
// Also remove from the failed builds queue in case it previously failed due to missing chunks // Also remove from the failed builds queue in case it previously failed due to missing chunks
ArbitraryDataBuildManager buildManager = ArbitraryDataBuildManager.getInstance(); ArbitraryDataBuildManager buildManager = ArbitraryDataBuildManager.getInstance();
if (buildManager.arbitraryDataFailedBuilds.containsKey(resourceId)) { if (buildManager.arbitraryDataFailedBuilds.containsKey(key)) {
buildManager.arbitraryDataFailedBuilds.remove(resourceId); buildManager.arbitraryDataFailedBuilds.remove(key);
} }
// Remove from the signature requests list now that we have all files for this signature // Remove from the signature requests list now that we have all files for this signature