|
|
@@ -17,6 +17,7 @@ import java.util.concurrent.ConcurrentHashMap;
|
|
|
import java.util.function.Function;
|
|
|
import java.util.function.Supplier;
|
|
|
|
|
|
+import net.ranides.assira.reflection.util.ClassUtils;
|
|
|
import net.ranides.assira.system.RuntimeUtils;
|
|
|
|
|
|
/**
|
|
|
@@ -26,11 +27,13 @@ public abstract class CacheMap<K,V> implements Cache<K,V> {
|
|
|
|
|
|
protected static final ReferenceQueue QUEUE = new ReferenceQueue<>();
|
|
|
|
|
|
- protected static final boolean USE_THREAD = RuntimeUtils.getProperty("assira.cache.no-threads", "false").equals("false");
|
|
|
+ protected static final boolean USE_THREAD = isThreadEnabled();
|
|
|
|
|
|
static {
|
|
|
if(USE_THREAD) {
|
|
|
- new Thread(CacheMap::cleanupThread, "assira cache cleanup").start();
|
|
|
+ Thread thread = new Thread(CacheMap::cleanupRemove, "assira cache cleanup");
|
|
|
+ thread.setDaemon(true);
|
|
|
+ thread.start();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -87,16 +90,28 @@ public abstract class CacheMap<K,V> implements Cache<K,V> {
|
|
|
|
|
|
protected abstract void iclear();
|
|
|
|
|
|
+ public static void cleanup(boolean block) {
|
|
|
+ if(block) {
|
|
|
+ cleanupRemove();
|
|
|
+ } else {
|
|
|
+ cleanupPoll();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
protected static void cleanup() {
|
|
|
if(!USE_THREAD) {
|
|
|
- KRef ref;
|
|
|
- while ( null != (ref = (KRef)QUEUE.poll()) ) {
|
|
|
- ref.src.iremove(ref.key);
|
|
|
- }
|
|
|
+ cleanupPoll();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- protected static void cleanupThread() {
|
|
|
+ protected static void cleanupPoll() {
|
|
|
+ KRef ref;
|
|
|
+ while ( null != (ref = (KRef)QUEUE.poll()) ) {
|
|
|
+ ref.src.iremove(ref.key);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ protected static void cleanupRemove() {
|
|
|
try {
|
|
|
while (true) {
|
|
|
KRef ref = (KRef) QUEUE.remove();
|
|
|
@@ -107,6 +122,19 @@ public abstract class CacheMap<K,V> implements Cache<K,V> {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ protected static boolean isThreadEnabled() {
|
|
|
+ if( !RuntimeUtils.getProperty("assira.cache.use-thread", "false").equals("true")) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ // check if there is cleaner for Java EE
|
|
|
+ try {
|
|
|
+ ClassUtils.forName("net.ranides.assira.ee.CacheMapCleaner");
|
|
|
+ return false;
|
|
|
+ } catch (ClassNotFoundException cause) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
private static final class SimpleCache<K,V> extends CacheMap<K,V> {
|
|
|
|
|
|
private final Map<K,KRef<K,V>> map = new ConcurrentHashMap<>();
|