No cache locks

This commit is contained in:
cyberpwn 2021-09-23 04:32:49 -04:00
parent 08e2244975
commit 56723330b3
2 changed files with 12 additions and 5 deletions

View File

@ -19,6 +19,7 @@
package com.volmit.iris.core.pregenerator; package com.volmit.iris.core.pregenerator;
import com.volmit.iris.Iris; import com.volmit.iris.Iris;
import com.volmit.iris.core.gui.PregeneratorJob;
import com.volmit.iris.util.collection.KList; import com.volmit.iris.util.collection.KList;
import com.volmit.iris.util.collection.KMap; import com.volmit.iris.util.collection.KMap;
import com.volmit.iris.util.collection.KSet; import com.volmit.iris.util.collection.KSet;

View File

@ -27,20 +27,25 @@ import java.util.function.Function;
public class KCache<K,V> implements MeteredCache { public class KCache<K,V> implements MeteredCache {
private long max; private long max;
private CacheLoader<? super K, ? extends V> loader; private CacheLoader<K, V> loader;
private LoadingCache<K, V> cache; private LoadingCache<K, V> cache;
public KCache(CacheLoader<K, V> loader, long max) public KCache(CacheLoader<K, V> loader, long max)
{ {
this.max = max; this.max = max;
this.loader = loader; this.loader = loader;
this.cache = Caffeine this.cache = create(loader);
}
private LoadingCache<K,V> create(CacheLoader<K,V> loader) {
return Caffeine
.newBuilder() .newBuilder()
.maximumSize(max) .maximumSize(max)
.build((k) -> loader == null ? null : loader.load(k)); .build((k) -> loader == null ? null : loader.load(k));
} }
public void setLoader(CacheLoader<? super K, ? extends V> loader)
public void setLoader(CacheLoader<K, V> loader)
{ {
this.loader = loader; this.loader = loader;
} }
@ -52,8 +57,9 @@ public class KCache<K,V> implements MeteredCache {
public void invalidate() public void invalidate()
{ {
cache.invalidateAll(); LoadingCache<?,?> c = cache;
cache.cleanUp(); cache = create(loader);
c.invalidateAll();
} }
public V get(K k) public V get(K k)