diff src/goodjava/webserver/handlers/DomainHandler.java @ 1498:1b809d2fdf03

add CacheMap
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 08 May 2020 18:07:14 -0600
parents 42c07ecb0ddc
children de5a81327d9c
line wrap: on
line diff
--- 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);
 			}