From 0e12a59f23375729d4c19d016f4ee756d42b28f6 Mon Sep 17 00:00:00 2001 From: Stepan Usatiuk Date: Wed, 2 Apr 2025 15:35:02 +0200 Subject: [PATCH] Objects: a little lazier caching --- .../stores/CachingObjectPersistentStore.java | 35 ++++++++++--------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/dhfs-parent/objects/src/main/java/com/usatiuk/objects/stores/CachingObjectPersistentStore.java b/dhfs-parent/objects/src/main/java/com/usatiuk/objects/stores/CachingObjectPersistentStore.java index a1916e7b..59429425 100644 --- a/dhfs-parent/objects/src/main/java/com/usatiuk/objects/stores/CachingObjectPersistentStore.java +++ b/dhfs-parent/objects/src/main/java/com/usatiuk/objects/stores/CachingObjectPersistentStore.java @@ -19,6 +19,7 @@ import javax.annotation.Nonnull; import java.util.Optional; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; +import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicReference; @ApplicationScoped @@ -64,6 +65,9 @@ public class CachingObjectPersistentStore { private final AtomicReference _cache; 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) { _cache = new AtomicReference<>( @@ -78,12 +82,14 @@ public class CachingObjectPersistentStore { _commitExecutor = Executors.newSingleThreadExecutor(); if (printStats) { - ExecutorService _statusExecutor = Executors.newSingleThreadExecutor(); + _statusExecutor = Executors.newSingleThreadExecutor(); _statusExecutor.submit(() -> { try { while (true) { - Log.debugv("Cache status: size=" + _cache.get().size() / 1024 / 1024 + "MB"); - Thread.sleep(10000); + Log.infov("Cache status: size=" + _cache.get().size() / 1024 / 1024 + "MB" + " cache success ratio: " + (_cached.get() / (double) _cacheTries.get())); + _cached.set(0); + _cacheTries.set(0); + Thread.sleep(1000); } } catch (InterruptedException ignored) { } @@ -136,26 +142,23 @@ public class CachingObjectPersistentStore { Cache finalCurCache = curCache; return new Snapshot() { private boolean _invalid = false; - private Cache _curCache = finalCurCache; + private final Cache _curCache = finalCurCache; private final Snapshot _backing = finalBacking; private void maybeCache(JObjectKey key, Optional obj) { + _cacheTries.incrementAndGet(); if (_invalid) return; - for (int i = 0; i < 10; i++) { - var globalCache = _cache.get(); - if (globalCache.version() != _curCache.version()) { - _invalid = true; - return; - } - - var newCache = globalCache.withPut(key, obj); - if (!_cache.compareAndSet(globalCache, newCache)) - continue; - - _curCache = newCache; + var globalCache = _cache.get(); + if (globalCache.version() != _curCache.version()) { + _invalid = true; + return; } + + var newCache = globalCache.withPut(key, obj); + if (_cache.compareAndSet(globalCache, newCache)) + _cached.incrementAndGet(); } @Override