Objects: a little lazier caching

This commit is contained in:
2025-04-02 15:35:02 +02:00
parent 735dd605d7
commit 0e12a59f23

View File

@@ -19,6 +19,7 @@ import javax.annotation.Nonnull;
import java.util.Optional; import java.util.Optional;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
@ApplicationScoped @ApplicationScoped
@@ -64,6 +65,9 @@ public class CachingObjectPersistentStore {
private final AtomicReference<Cache> _cache; private final AtomicReference<Cache> _cache;
private ExecutorService _commitExecutor; private ExecutorService _commitExecutor;
private ExecutorService _statusExecutor;
private AtomicLong _cached = new AtomicLong();
private AtomicLong _cacheTries = new AtomicLong();
public CachingObjectPersistentStore(@ConfigProperty(name = "dhfs.objects.lru.limit") int sizeLimit) { public CachingObjectPersistentStore(@ConfigProperty(name = "dhfs.objects.lru.limit") int sizeLimit) {
_cache = new AtomicReference<>( _cache = new AtomicReference<>(
@@ -78,12 +82,14 @@ public class CachingObjectPersistentStore {
_commitExecutor = Executors.newSingleThreadExecutor(); _commitExecutor = Executors.newSingleThreadExecutor();
if (printStats) { if (printStats) {
ExecutorService _statusExecutor = Executors.newSingleThreadExecutor(); _statusExecutor = Executors.newSingleThreadExecutor();
_statusExecutor.submit(() -> { _statusExecutor.submit(() -> {
try { try {
while (true) { while (true) {
Log.debugv("Cache status: size=" + _cache.get().size() / 1024 / 1024 + "MB"); Log.infov("Cache status: size=" + _cache.get().size() / 1024 / 1024 + "MB" + " cache success ratio: " + (_cached.get() / (double) _cacheTries.get()));
Thread.sleep(10000); _cached.set(0);
_cacheTries.set(0);
Thread.sleep(1000);
} }
} catch (InterruptedException ignored) { } catch (InterruptedException ignored) {
} }
@@ -136,26 +142,23 @@ public class CachingObjectPersistentStore {
Cache finalCurCache = curCache; Cache finalCurCache = curCache;
return new Snapshot<JObjectKey, JDataVersionedWrapper>() { return new Snapshot<JObjectKey, JDataVersionedWrapper>() {
private boolean _invalid = false; private boolean _invalid = false;
private Cache _curCache = finalCurCache; private final Cache _curCache = finalCurCache;
private final Snapshot<JObjectKey, JDataVersionedWrapper> _backing = finalBacking; private final Snapshot<JObjectKey, JDataVersionedWrapper> _backing = finalBacking;
private void maybeCache(JObjectKey key, Optional<JDataVersionedWrapper> obj) { private void maybeCache(JObjectKey key, Optional<JDataVersionedWrapper> obj) {
_cacheTries.incrementAndGet();
if (_invalid) if (_invalid)
return; return;
for (int i = 0; i < 10; i++) { var globalCache = _cache.get();
var globalCache = _cache.get(); if (globalCache.version() != _curCache.version()) {
if (globalCache.version() != _curCache.version()) { _invalid = true;
_invalid = true; return;
return;
}
var newCache = globalCache.withPut(key, obj);
if (!_cache.compareAndSet(globalCache, newCache))
continue;
_curCache = newCache;
} }
var newCache = globalCache.withPut(key, obj);
if (_cache.compareAndSet(globalCache, newCache))
_cached.incrementAndGet();
} }
@Override @Override