Added ObjectCache class and fixed som hashmap iterator issues
This commit is contained in:
parent
6dd2210be1
commit
2fcc8e98c5
6 changed files with 252 additions and 19 deletions
5
src/zutil/Timer.java
Normal file → Executable file
5
src/zutil/Timer.java
Normal file → Executable file
|
|
@ -44,9 +44,12 @@ public class Timer {
|
|||
|
||||
/**
|
||||
* Will start or restart the timer if it is already running
|
||||
*
|
||||
* @return a reference of itself
|
||||
*/
|
||||
public void start(){
|
||||
public Timer start(){
|
||||
timestamp = System.currentTimeMillis();
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -156,18 +156,17 @@ public class DBBeanSQLResultHandler<T> implements SQLResultHandler<T>{
|
|||
}
|
||||
|
||||
int removed = 0;
|
||||
long time = System.currentTimeMillis();
|
||||
for(Object classKey : cache.keySet()){
|
||||
if( classKey == null ) continue;
|
||||
|
||||
Map<Long,DBBeanCache> class_cache = cache.get(classKey);
|
||||
for(Object objKey : class_cache.keySet()){
|
||||
if( objKey == null ) continue;
|
||||
|
||||
DBBeanCache beanCache = class_cache.get(objKey);
|
||||
for(Iterator<Map.Entry<Long, DBBeanCache>> it = class_cache.entrySet().iterator(); it.hasNext(); ) {
|
||||
Map.Entry<Long, DBBeanCache> entry = it.next();
|
||||
if( entry.getKey() == null ) continue;
|
||||
|
||||
// Check if session is still valid
|
||||
if( beanCache.bean.get() == null ){
|
||||
class_cache.remove(objKey);
|
||||
if( entry.getValue().bean.get() == null ){
|
||||
it.remove();
|
||||
removed++;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
91
src/zutil/struct/ObjectCache.java
Executable file
91
src/zutil/struct/ObjectCache.java
Executable file
|
|
@ -0,0 +1,91 @@
|
|||
package zutil.struct;
|
||||
|
||||
import zutil.Timer;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* This is a Object cache where objects can be stored with {@link java.lang.ref.WeakReference} and a time limit.
|
||||
*
|
||||
* Created by Ziver on 2016-07-29.
|
||||
*/
|
||||
public class ObjectCache<K, V> {
|
||||
|
||||
private static class CacheEntry<V>{
|
||||
public Timer timer;
|
||||
public WeakReference<V> value;
|
||||
}
|
||||
|
||||
private HashMap<K, CacheEntry<V>> cache = new HashMap<>();
|
||||
private long ttl;
|
||||
|
||||
|
||||
public ObjectCache(long ttl){
|
||||
this.ttl = ttl;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Stores a key and value pair in the cache.
|
||||
*/
|
||||
public void put(K key, V value){
|
||||
CacheEntry<V> entry = new CacheEntry<>();
|
||||
entry.timer = new Timer(ttl).start();
|
||||
entry.value = new WeakReference<>(value);
|
||||
cache.put(key, entry);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Checks if the specific key is available in
|
||||
* the cache and that it is valid.
|
||||
*/
|
||||
public boolean containsKey(Object key){
|
||||
if(cache.containsKey(key)){
|
||||
CacheEntry<V> entry = cache.get(key);
|
||||
if (entry.timer.hasTimedOut() || entry.value.get() == null) // entry to old or not valid
|
||||
cache.remove(key);
|
||||
else
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public V get(Object key){
|
||||
if (containsKey(key))
|
||||
return cache.get(key).value.get();
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* This method will return the number of stored entries(valid and timed out entries) in the cache.
|
||||
*/
|
||||
public int size() {
|
||||
return cache.size();
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Iterates through the Set and removes all entries that has passed its TTL.
|
||||
*
|
||||
* @return the number of objects removed from the Set
|
||||
*/
|
||||
public int garbageCollect(){
|
||||
int count = 0;
|
||||
for(Iterator<Map.Entry<K, CacheEntry<V>>> it = cache.entrySet().iterator(); it.hasNext(); ) {
|
||||
Map.Entry<K, CacheEntry<V>> mapEntry = it.next();
|
||||
if (mapEntry.getValue().timer.hasTimedOut() || mapEntry.getValue().value.get() == null) { // entry to old or not valid
|
||||
it.remove();
|
||||
++count;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
}
|
||||
|
|
@ -24,14 +24,18 @@
|
|||
|
||||
package zutil.struct;
|
||||
|
||||
import zutil.Timer;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* This is a timed HashSet. Each entry has a limited time to live.
|
||||
*/
|
||||
public class TimedHashSet<T> {
|
||||
|
||||
private HashMap<T, Long> map;
|
||||
private HashMap<T, Timer> map;
|
||||
private long ttl;
|
||||
|
||||
/**
|
||||
|
|
@ -47,12 +51,12 @@ public class TimedHashSet<T> {
|
|||
* @return true if the object already existed in the set which will reset the TTL.
|
||||
*/
|
||||
public boolean add(T o){
|
||||
return map.put(o, System.currentTimeMillis()) != null;
|
||||
return map.put(o, new Timer(ttl).start()) != null;
|
||||
}
|
||||
|
||||
public boolean contains(Object o){
|
||||
if(map.containsKey(o)){
|
||||
if(map.get(o) + ttl < System.currentTimeMillis()) // entry to old
|
||||
if(map.get(o).hasTimedOut()) // entry to old
|
||||
map.remove(o);
|
||||
else
|
||||
return true;
|
||||
|
|
@ -62,12 +66,28 @@ public class TimedHashSet<T> {
|
|||
|
||||
|
||||
/**
|
||||
* Iterates through the Set and removes all entries that has passed the TTL
|
||||
* This method will return the number of stored entries(valid and timed out entries) in the Set.
|
||||
*/
|
||||
public void garbageCollect(){
|
||||
for(T o : map.keySet()){
|
||||
if(map.get(o) + ttl < System.currentTimeMillis()) // entry to old
|
||||
map.remove(o);
|
||||
}
|
||||
public int size() {
|
||||
return map.size();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Iterates through the Set and removes all entries that has passed its TTL.
|
||||
*
|
||||
* @return the number of objects removed from the Set
|
||||
*/
|
||||
public int garbageCollect(){
|
||||
int count = 0;
|
||||
for(Iterator<Map.Entry<T, Timer>> it = map.entrySet().iterator(); it.hasNext(); ) {
|
||||
Map.Entry<T, Timer> entry = it.next();
|
||||
if (entry.getValue().hasTimedOut()) { // entry to old
|
||||
it.remove();
|
||||
++count;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue