Removed DBBean garbage collector
it it is not needed with WeakReference anymore
This commit is contained in:
parent
0ba153b118
commit
f8a46f2089
1 changed files with 27 additions and 85 deletions
|
|
@ -17,17 +17,15 @@ import java.util.logging.Logger;
|
|||
*/
|
||||
class DBBeanCache {
|
||||
private static final Logger logger = LogUtil.getLogger();
|
||||
/** This is the time to live for the cached items **/
|
||||
/**
|
||||
* This is the time to live for the cached items
|
||||
**/
|
||||
public static final long CACHE_DATA_TTL = 1000 * 60 * 5; // 5 min in ms
|
||||
/** A cache for detecting recursion **/
|
||||
/**
|
||||
* A cache for detecting recursion
|
||||
**/
|
||||
private static Map<Class<?>, Map<Long, CacheItem>> cache =
|
||||
new ConcurrentHashMap<>();
|
||||
private static ScheduledExecutorService executor;
|
||||
|
||||
|
||||
static {
|
||||
enableBeanGBC(true); // Initiate DBBeanGarbageCollector
|
||||
}
|
||||
|
||||
/**
|
||||
* A cache container that contains a bean and its last filed update time
|
||||
|
|
@ -37,74 +35,16 @@ class DBBeanCache {
|
|||
public WeakReference<DBBean> bean;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* This function cancels the internal cache garbage collector in DBBean.
|
||||
* GBC is enabled by default
|
||||
*/
|
||||
public static synchronized void enableBeanGBC(boolean enable){
|
||||
if(enable){
|
||||
if( executor == null ){
|
||||
executor = Executors.newSingleThreadScheduledExecutor();
|
||||
executor.scheduleAtFixedRate( new DBBeanGarbageCollector(), CACHE_DATA_TTL, CACHE_DATA_TTL *2, TimeUnit.MILLISECONDS );
|
||||
logger.fine("Bean garbage collection daemon enabled");
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (executor != null) {
|
||||
executor.shutdown();
|
||||
executor = null;
|
||||
logger.fine("Bean garbage collection daemon disabled");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This class acts as an garbage collector that removes old DBBeans
|
||||
*/
|
||||
private static class DBBeanGarbageCollector implements Runnable {
|
||||
public void run() {
|
||||
try {
|
||||
if (cache == null) {
|
||||
logger.severe("DBBeanSQLResultHandler not initialized, stopping DBBeanGarbageCollector timer.");
|
||||
enableBeanGBC(false);
|
||||
return;
|
||||
}
|
||||
|
||||
int removed = 0;
|
||||
for (Object classKey : cache.keySet()) {
|
||||
if (classKey == null) continue;
|
||||
|
||||
Map<Long, CacheItem> class_cache = cache.get(classKey);
|
||||
for (Iterator<Map.Entry<Long, CacheItem>> it = class_cache.entrySet().iterator(); it.hasNext(); ) {
|
||||
Map.Entry<Long, CacheItem> entry = it.next();
|
||||
if (entry.getKey() == null) continue;
|
||||
// Check if session is still valid
|
||||
if (entry.getValue().bean.get() == null) {
|
||||
it.remove();
|
||||
removed++;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (removed > 0)
|
||||
logger.info("DBBean GarbageCollector has cleared " + removed + " beans from cache.");
|
||||
} catch (Exception e) {
|
||||
logger.log(Level.SEVERE, "DBBeanGarbageCollector thread has crashed", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static boolean contains(DBBean obj) {
|
||||
if (obj == null)
|
||||
return false;
|
||||
return contains(obj.getClass(), obj.getId());
|
||||
}
|
||||
|
||||
public static boolean contains(Class<?> c, Long id) {
|
||||
if (cache.containsKey(c)) {
|
||||
CacheItem cacheItem = cache.get(c).get(id);
|
||||
|
||||
// Check if the cache is valid
|
||||
if (cacheItem != null && cacheItem.bean.get() != null) {
|
||||
return true;
|
||||
|
|
@ -152,9 +92,11 @@ class DBBeanCache {
|
|||
cache.get(obj.getClass()).get(obj.getId()).updateTimestamp = System.currentTimeMillis();
|
||||
return;
|
||||
}
|
||||
|
||||
CacheItem cacheItem = new CacheItem();
|
||||
cacheItem.updateTimestamp = System.currentTimeMillis();
|
||||
cacheItem.bean = new WeakReference<>(obj);
|
||||
|
||||
if (cache.containsKey(obj.getClass()))
|
||||
cache.get(obj.getClass()).put(obj.getId(), cacheItem);
|
||||
else {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue