jsr354-ri icon indicating copy to clipboard operation
jsr354-ri copied to clipboard

Cache invalidation on VM restart due to non-persisted lastLoaded timestamp

Open infernap12 opened this issue 11 months ago • 1 comments

Issue

The lastLoaded timestamp in the LoadableHttpResource class is not persisted to disk along with the cached data. When the VM is restarted, this timestamp is reset to its default value of 0, causing the cache to be considered invalid and triggering unnecessary remote server requests.

Current Behavior

In LoadableHttpResource.java, the load() method checks if the cache is valid using:

if ((lastLoaded + cacheTTLMillis) <= System.currentTimeMillis()) {
    clearCache();
}

When data is loaded from a remote source, the timestamp is updated:

// In load(URI itemToLoad, boolean fallbackLoad) method
if (!fallbackLoad) {
    writeCache();
    lastLoaded = System.currentTimeMillis();
    loadCount.incrementAndGet();
}

However, the writeCache() method only writes the data to disk, not the timestamp:

protected void writeCache() throws IOException {
    if (this.cache != null) {
        byte[] data = this.data == null ? null : this.data.get();
        if (data == null) {
            return;
        }
        this.cache.write(resourceId, data);
    }
}

This means that on VM restart, lastLoaded is reset to 0, and the condition (lastLoaded + cacheTTLMillis) <= System.currentTimeMillis() will always be true, causing the cache to be considered invalid.

Expected Behavior

The lastLoaded timestamp should be persisted to disk along with the cached data, so that on VM restart, the system can determine if the cache is still valid based on the actual time it was last loaded.

infernap12 avatar May 18 '25 13:05 infernap12

Willing to investigate an avenue for remediating this, but didn't want to pursue an avenue that would be unacceptable.

Would updating the ResourceCache interface be acceptable?

infernap12 avatar May 18 '25 13:05 infernap12