Skip to content

Commit

Permalink
Report inconsistencies between derivative cache linker and compute() …
Browse files Browse the repository at this point in the history
…early
  • Loading branch information
robertvazan committed Jan 20, 2024
1 parent ed6272b commit 07383de
Showing 1 changed file with 33 additions and 29 deletions.
62 changes: 33 additions & 29 deletions src/main/java/com/machinezoo/foxcache/CachedData.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,37 @@

@CodeIssue("Downgrade softref to weakref after one second. Anything that needs longer caching should be persisted.")
class CachedData {
static final ConcurrentMap<LazyCache<?>, Object> lazy = new ConcurrentHashMap<>();
/*
* Soft-valued cache may cause extremely inefficient GC behavior:
* https://bugs.openjdk.java.net/browse/JDK-6912889
*
* It is however very simple and it will use all RAM that is allocated to Java process,
* which is usually some fraction of physical RAM.
* This cache can be tuned indirectly with -Xmx and -XX:SoftRefLRUPolicyMSPerMB.
*
* Cached value is wrapped in Optional, because Guava cache does not tolerate null values.
*/
static final LoadingCache<ComputeCache<?>, Optional<?>> compute = CacheBuilder.newBuilder()
.softValues()
.build(CacheLoader.from(k -> Optional.ofNullable(k.compute())));
/*
* Like in ComputeCaches, just specialized for DerivativeCache.
*/
static final LoadingCache<DerivativeCache<?>, ReactiveLazy<CacheDerivative<Object>>> derivative = CacheBuilder.newBuilder()
.softValues()
.build(CacheLoader.from(k -> materialize(k)));
private static <T> ReactiveLazy<CacheDerivative<Object>> materialize(DerivativeCache<T> cache) {
return new ReactiveLazy<>(() -> CacheDerivative.capture(() -> {
/*
* Touch the cache just in case there are extra dependencies there.
*/
cache.touch();
return cache.compute();
}));
}
static final ConcurrentMap<LazyCache<?>, Object> lazy = new ConcurrentHashMap<>();
/*
* Soft-valued cache may cause extremely inefficient GC behavior:
* https://bugs.openjdk.java.net/browse/JDK-6912889
*
* It is however very simple and it will use all RAM that is allocated to Java process,
* which is usually some fraction of physical RAM.
* This cache can be tuned indirectly with -Xmx and -XX:SoftRefLRUPolicyMSPerMB.
*
* Cached value is wrapped in Optional, because Guava cache does not tolerate null values.
*/
static final LoadingCache<ComputeCache<?>, Optional<?>> compute = CacheBuilder.newBuilder()
.softValues()
.build(CacheLoader.from(k -> Optional.ofNullable(k.compute())));
/*
* Like in ComputeCaches, just specialized for DerivativeCache.
*/
static final LoadingCache<DerivativeCache<?>, ReactiveLazy<CacheDerivative<Object>>> derivative = CacheBuilder.newBuilder()
.softValues()
.build(CacheLoader.from(k -> materialize(k)));
private static <T> ReactiveLazy<CacheDerivative<Object>> materialize(DerivativeCache<T> cache) {
return new ReactiveLazy<>(() -> CacheDerivative.capture(() -> {
/*
* Touch the cache just in case there are extra dependencies there.
*/
cache.touch();
/*
* Report inconsistencies between linker and compute() early.
*/
CacheInput.get().freeze();
return cache.compute();
}));
}
}

0 comments on commit 07383de

Please sign in to comment.