Made sure json memory cache is never null

Affects issues:
- #1814
This commit is contained in:
Risto Lahtela 2021-03-25 07:52:51 +02:00
parent 42827d95b9
commit 4f39481549
2 changed files with 17 additions and 12 deletions

View File

@ -49,35 +49,40 @@ public class JSONMemoryStorageShim implements JSONStorage {
@Override
public StoredJSON storeJson(String identifier, String json, long timestamp) {
StoredJSON storedJSON = underlyingStorage.storeJson(identifier, json, timestamp);
cache.put(new TimestampedIdentifier(identifier, timestamp), storedJSON);
getCache().put(new TimestampedIdentifier(identifier, timestamp), storedJSON);
return storedJSON;
}
public Cache<TimestampedIdentifier, StoredJSON> getCache() {
if (cache == null) enable();
return cache;
}
@Override
public Optional<StoredJSON> fetchJSON(String identifier) {
for (Map.Entry<TimestampedIdentifier, StoredJSON> entry : cache.asMap().entrySet()) {
for (Map.Entry<TimestampedIdentifier, StoredJSON> entry : getCache().asMap().entrySet()) {
if (entry.getKey().identifier.equalsIgnoreCase(identifier)) {
return Optional.of(entry.getValue());
}
}
Optional<StoredJSON> found = underlyingStorage.fetchJSON(identifier);
found.ifPresent(storedJSON -> cache.put(new TimestampedIdentifier(identifier, storedJSON.timestamp), storedJSON));
found.ifPresent(storedJSON -> getCache().put(new TimestampedIdentifier(identifier, storedJSON.timestamp), storedJSON));
return found;
}
@Override
public Optional<StoredJSON> fetchExactJson(String identifier, long timestamp) {
StoredJSON cached = cache.getIfPresent(new TimestampedIdentifier(identifier, timestamp));
StoredJSON cached = getCache().getIfPresent(new TimestampedIdentifier(identifier, timestamp));
if (cached != null) return Optional.of(cached);
Optional<StoredJSON> found = underlyingStorage.fetchExactJson(identifier, timestamp);
found.ifPresent(storedJSON -> cache.put(new TimestampedIdentifier(identifier, timestamp), storedJSON));
found.ifPresent(storedJSON -> getCache().put(new TimestampedIdentifier(identifier, timestamp), storedJSON));
return found;
}
@Override
public Optional<StoredJSON> fetchJsonMadeBefore(String identifier, long timestamp) {
for (Map.Entry<TimestampedIdentifier, StoredJSON> entry : cache.asMap().entrySet()) {
for (Map.Entry<TimestampedIdentifier, StoredJSON> entry : getCache().asMap().entrySet()) {
TimestampedIdentifier key = entry.getKey();
if (key.timestamp < timestamp && key.identifier.equalsIgnoreCase(identifier)) {
return Optional.of(entry.getValue());
@ -85,32 +90,32 @@ public class JSONMemoryStorageShim implements JSONStorage {
}
Optional<StoredJSON> found = underlyingStorage.fetchJsonMadeBefore(identifier, timestamp);
found.ifPresent(storedJSON -> cache.put(new TimestampedIdentifier(identifier, storedJSON.timestamp), storedJSON));
found.ifPresent(storedJSON -> getCache().put(new TimestampedIdentifier(identifier, storedJSON.timestamp), storedJSON));
return found;
}
@Override
public Optional<StoredJSON> fetchJsonMadeAfter(String identifier, long timestamp) {
for (Map.Entry<TimestampedIdentifier, StoredJSON> entry : cache.asMap().entrySet()) {
for (Map.Entry<TimestampedIdentifier, StoredJSON> entry : getCache().asMap().entrySet()) {
TimestampedIdentifier key = entry.getKey();
if (key.timestamp > timestamp && key.identifier.equalsIgnoreCase(identifier)) {
return Optional.of(entry.getValue());
}
}
Optional<StoredJSON> found = underlyingStorage.fetchJsonMadeAfter(identifier, timestamp);
found.ifPresent(storedJSON -> cache.put(new TimestampedIdentifier(identifier, storedJSON.timestamp), storedJSON));
found.ifPresent(storedJSON -> getCache().put(new TimestampedIdentifier(identifier, storedJSON.timestamp), storedJSON));
return found;
}
@Override
public void invalidateOlder(String identifier, long timestamp) {
Set<TimestampedIdentifier> toInvalidate = new HashSet<>();
for (TimestampedIdentifier key : cache.asMap().keySet()) {
for (TimestampedIdentifier key : getCache().asMap().keySet()) {
if (key.timestamp < timestamp && key.identifier.equalsIgnoreCase(identifier)) {
toInvalidate.add(key);
}
}
toInvalidate.forEach(cache::invalidate);
toInvalidate.forEach(getCache()::invalidate);
underlyingStorage.invalidateOlder(identifier, timestamp);
}

View File

@ -47,7 +47,7 @@ public class WebserverSettings {
public static final Setting<Long> REDUCED_REFRESH_BARRIER = new TimeSetting("Webserver.Cache.Reduced_refresh_barrier");
public static final Setting<Long> INVALIDATE_QUERY_RESULTS = new TimeSetting("Webserver.Cache.Invalidate_query_results_on_disk_after");
public static final Setting<Long> INVALIDATE_DISK_CACHE = new TimeSetting("Webserver.Cache.Invalidate_disk_cache_after");
public static final Setting<Long> INVALIDATE_MEMORY_CACHE = new TimeSetting("Webserver.Cache.Invalidate_memory_cache_after");
public static final Setting<Long> INVALIDATE_MEMORY_CACHE = new TimeSetting("Webserver.Cache.Invalidate_memory_cache_after", TimeUnit.MINUTES.toMillis(5L));
public static final Setting<Long> COOKIES_EXPIRE_AFTER = new TimeSetting("Webserver.Security.Cookies_expire_after", TimeUnit.HOURS.toMillis(2L));
private WebserverSettings() {