changeset 1498:1b809d2fdf03

add CacheMap
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 08 May 2020 18:07:14 -0600
parents f04bfbb08721
children 22e15cf73040
files src/goodjava/util/CacheMap.java src/goodjava/util/SoftCacheMap.java src/goodjava/util/WeakCacheMap.java src/goodjava/webserver/handlers/DomainHandler.java
diffstat 4 files changed, 231 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
diff -r f04bfbb08721 -r 1b809d2fdf03 src/goodjava/util/CacheMap.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/goodjava/util/CacheMap.java	Fri May 08 18:07:14 2020 -0600
@@ -0,0 +1,156 @@
+package goodjava.util;
+
+import java.lang.ref.ReferenceQueue;
+import java.util.Map;
+import java.util.AbstractMap;
+import java.util.HashMap;
+import java.util.Set;
+import java.util.AbstractSet;
+import java.util.Iterator;
+
+
+public abstract class CacheMap<K,V> extends AbstractMap<K,V> {
+
+	protected static interface MyReference<K,V> {
+		public K key();
+		public V get();
+		public void clear();
+	}
+
+	protected abstract MyReference<K,V> newReference(K key,V value,ReferenceQueue<V> q);
+ 
+	private final Map<K,MyReference<K,V>> cache = new HashMap<K,MyReference<K,V>>();
+	private final ReferenceQueue<V> queue = new ReferenceQueue<V>();
+
+	private void sweep() {
+		while(true) {
+			@SuppressWarnings("unchecked")
+			MyReference<K,V> ref = (MyReference<K,V>)queue.poll();
+			if( ref == null )
+				return;
+			MyReference<K,V> mappedRef = cache.remove(ref.key());
+			if( mappedRef != ref && mappedRef != null )
+				cache.put( mappedRef.key(), mappedRef );  // put it back
+		}
+	}
+
+	public int size() {
+		return cache.size();
+	}
+
+	public boolean isEmpty() {
+		return cache.isEmpty();
+	}
+
+	public boolean containsKey(Object key) {
+		return cache.containsKey(key);
+	}
+
+	public V get(Object key) {
+		MyReference<K,V> ref = cache.get(key);
+		return ref==null ? null : ref.get();
+	}
+
+	public V put(K key,V value) {
+		sweep();
+		MyReference<K,V> ref = cache.put( key, newReference(key,value,queue) );
+		return ref==null ? null : ref.get();
+	}
+
+	public V remove(Object key) {
+		sweep();
+		MyReference<K,V> ref = cache.remove(key);
+		return ref==null ? null : ref.get();
+	}
+
+	public void clear() {
+		sweep();
+		cache.clear();
+	}
+
+/*
+	public Object clone() {
+		GCCacheMap map = new GCCacheMap();
+		map.cache = (HashMap)cache.clone();
+		return map;
+	}
+*/
+	public Set<K> keySet() {
+		return cache.keySet();
+	}
+
+	public Set<Map.Entry<K,V>> entrySet() {
+		return new MySet();
+	}
+
+
+	private class MySet extends AbstractSet<Map.Entry<K,V>> {
+
+		public int size() {
+			return CacheMap.this.size();
+		}
+
+		public Iterator<Map.Entry<K,V>> iterator() {
+			return new MyIterator(cache.entrySet().iterator());
+		}
+
+	}
+
+	private class MyIterator implements Iterator<Map.Entry<K,V>> {
+		Iterator<Map.Entry<K,MyReference<K,V>>> iter;
+
+		MyIterator(Iterator<Map.Entry<K,MyReference<K,V>>> iter) {
+			this.iter = iter;
+		}
+
+		public boolean hasNext() {
+			return iter.hasNext();
+		}
+
+		public void remove() {
+			iter.remove();
+		}
+
+		public Map.Entry<K,V> next() {
+			return new MyEntry( iter.next() );
+		}
+	}
+
+	private class MyEntry implements Map.Entry<K,V> {
+		Map.Entry<K,MyReference<K,V>> entry;
+
+		MyEntry(Map.Entry<K,MyReference<K,V>> entry) {
+			this.entry = entry;
+		}
+
+		public K getKey() {
+			return entry.getKey();
+		}
+
+		public V getValue() {
+			MyReference<K,V> ref = entry.getValue();
+			return ref.get();
+		}
+
+		public V setValue(V value) {
+			MyReference<K,V> ref = entry.setValue( newReference(getKey(),value,queue) );
+			return ref.get();
+		}
+
+		public boolean equals(Object o) {
+			if( o==null || !(o instanceof CacheMap.MyEntry) )
+				return false;
+			@SuppressWarnings("unchecked")
+			MyEntry m = (MyEntry)o;
+			return entry.equals(m.entry);
+		}
+
+		public int hashCode() {
+			K key = getKey();
+			V value = getValue();
+			return (key==null ? 0 : key.hashCode()) ^
+					(value==null ? 0 : value.hashCode());
+		}
+	}
+
+}
diff -r f04bfbb08721 -r 1b809d2fdf03 src/goodjava/util/SoftCacheMap.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/goodjava/util/SoftCacheMap.java	Fri May 08 18:07:14 2020 -0600
@@ -0,0 +1,35 @@
+package goodjava.util;
+
+import java.lang.ref.ReferenceQueue;
+import java.lang.ref.SoftReference;
+
+
+public final class SoftCacheMap<K,V> extends CacheMap<K,V> {
+
+	static final class MySoftReference<K,V> extends SoftReference<V> implements MyReference<K,V> {
+		private final K key;
+
+		MySoftReference(K key,V value,ReferenceQueue<V> q) {
+			super(value,q);
+			this.key = key;
+		}
+
+		public K key() {
+			return key;
+		}
+
+		public boolean equals(Object obj) {
+			Object o = this.get();
+			if( o==null )
+				return false;
+			SoftReference ref = (SoftReference)obj;
+			return o.equals(ref.get());
+		}
+
+	}
+
+	protected MyReference<K,V> newReference(K key,V value,ReferenceQueue<V> q) {
+		return new MySoftReference<K,V>(key,value,q);
+	}
+
+}
diff -r f04bfbb08721 -r 1b809d2fdf03 src/goodjava/util/WeakCacheMap.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/goodjava/util/WeakCacheMap.java	Fri May 08 18:07:14 2020 -0600
@@ -0,0 +1,35 @@
+package goodjava.util;
+
+import java.lang.ref.ReferenceQueue;
+import java.lang.ref.WeakReference;
+
+
+public class WeakCacheMap<K,V> extends CacheMap<K,V> {
+
+	static final class MyWeakReference<K,V> extends WeakReference<V> implements MyReference<K,V> {
+		private final K key;
+
+		MyWeakReference(K key,V value,ReferenceQueue<V> q) {
+			super(value,q);
+			this.key = key;
+		}
+
+		public K key() {
+			return key;
+		}
+
+		public boolean equals(Object obj) {
+			Object o = this.get();
+			if( o==null )
+				return false;
+			WeakReference ref = (WeakReference)obj;
+			return o.equals(ref.get());
+		}
+
+	}
+
+	protected MyReference<K,V> newReference(K key,V value,ReferenceQueue<V> q) {
+		return new MyWeakReference<K,V>(key,value,q);
+	}
+
+}
diff -r f04bfbb08721 -r 1b809d2fdf03 src/goodjava/webserver/handlers/DomainHandler.java
--- a/src/goodjava/webserver/handlers/DomainHandler.java	Fri May 08 16:54:30 2020 -0600
+++ b/src/goodjava/webserver/handlers/DomainHandler.java	Fri May 08 18:07:14 2020 -0600
@@ -2,16 +2,13 @@
 
 import java.io.Closeable;
 import java.io.IOException;
-import java.lang.ref.Reference;
-//import java.lang.ref.WeakReference;
-import java.lang.ref.SoftReference;
-import java.lang.ref.ReferenceQueue;
 import java.util.Map;
 import java.util.HashMap;
 import java.util.Set;
 import java.util.Timer;
 import java.util.TimerTask;
 import java.util.concurrent.ConcurrentHashMap;
+import goodjava.util.SoftCacheMap;
 import goodjava.logging.Logger;
 import goodjava.logging.LoggerFactory;
 import goodjava.webserver.Handler;
@@ -40,7 +37,7 @@
 	}
 
 	private static final long HOUR = 1000L*60*60;
-	private final Map<String,Reference<Handler>> map = new HashMap<String,Reference<Handler>>();
+	private final Map<String,Handler> map = new SoftCacheMap<String,Handler>();
 	private final Set<Handler> dontGc = ConcurrentHashMap.newKeySet();
 	private final Timer timer = new Timer();
 
@@ -71,14 +68,13 @@
 	public Handler getHandler(String domain) {
 		domain = domain.toLowerCase();
 		synchronized(map) {
-			Reference<Handler> ref = map.get(domain);
-			Handler handler = ref==null ? null : ref.get();
+			Handler handler = map.get(domain);
 			if( handler == null ) {
 				//if(ref!=null) logger.info("gc "+domain);
 				handler = factory.newHandler(domain);
 				if( handler == null )
 					return null;
-				map.put(domain,new SoftReference<Handler>(handler));
+				map.put(domain,handler);
 			}
 			return handler;
 		}
@@ -88,8 +84,7 @@
 		//logger.info("removeHandler "+domain);
 		domain = domain.toLowerCase();
 		synchronized(map) {
-			Reference<Handler> ref = map.remove(domain);
-			Handler handler = ref==null ? null : ref.get();
+			Handler handler = map.remove(domain);
 			if( handler != null ) {
 				close(handler);
 			}