comparison 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
comparison
equal deleted inserted replaced
1497:f04bfbb08721 1498:1b809d2fdf03
1 package goodjava.webserver.handlers; 1 package goodjava.webserver.handlers;
2 2
3 import java.io.Closeable; 3 import java.io.Closeable;
4 import java.io.IOException; 4 import java.io.IOException;
5 import java.lang.ref.Reference;
6 //import java.lang.ref.WeakReference;
7 import java.lang.ref.SoftReference;
8 import java.lang.ref.ReferenceQueue;
9 import java.util.Map; 5 import java.util.Map;
10 import java.util.HashMap; 6 import java.util.HashMap;
11 import java.util.Set; 7 import java.util.Set;
12 import java.util.Timer; 8 import java.util.Timer;
13 import java.util.TimerTask; 9 import java.util.TimerTask;
14 import java.util.concurrent.ConcurrentHashMap; 10 import java.util.concurrent.ConcurrentHashMap;
11 import goodjava.util.SoftCacheMap;
15 import goodjava.logging.Logger; 12 import goodjava.logging.Logger;
16 import goodjava.logging.LoggerFactory; 13 import goodjava.logging.LoggerFactory;
17 import goodjava.webserver.Handler; 14 import goodjava.webserver.Handler;
18 import goodjava.webserver.Request; 15 import goodjava.webserver.Request;
19 import goodjava.webserver.Response; 16 import goodjava.webserver.Response;
38 logger.info("dontGc.clear()"); 35 logger.info("dontGc.clear()");
39 } 36 }
40 } 37 }
41 38
42 private static final long HOUR = 1000L*60*60; 39 private static final long HOUR = 1000L*60*60;
43 private final Map<String,Reference<Handler>> map = new HashMap<String,Reference<Handler>>(); 40 private final Map<String,Handler> map = new SoftCacheMap<String,Handler>();
44 private final Set<Handler> dontGc = ConcurrentHashMap.newKeySet(); 41 private final Set<Handler> dontGc = ConcurrentHashMap.newKeySet();
45 private final Timer timer = new Timer(); 42 private final Timer timer = new Timer();
46 43
47 private final Factory factory; 44 private final Factory factory;
48 45
69 } 66 }
70 67
71 public Handler getHandler(String domain) { 68 public Handler getHandler(String domain) {
72 domain = domain.toLowerCase(); 69 domain = domain.toLowerCase();
73 synchronized(map) { 70 synchronized(map) {
74 Reference<Handler> ref = map.get(domain); 71 Handler handler = map.get(domain);
75 Handler handler = ref==null ? null : ref.get();
76 if( handler == null ) { 72 if( handler == null ) {
77 //if(ref!=null) logger.info("gc "+domain); 73 //if(ref!=null) logger.info("gc "+domain);
78 handler = factory.newHandler(domain); 74 handler = factory.newHandler(domain);
79 if( handler == null ) 75 if( handler == null )
80 return null; 76 return null;
81 map.put(domain,new SoftReference<Handler>(handler)); 77 map.put(domain,handler);
82 } 78 }
83 return handler; 79 return handler;
84 } 80 }
85 } 81 }
86 82
87 public void removeHandler(String domain) { 83 public void removeHandler(String domain) {
88 //logger.info("removeHandler "+domain); 84 //logger.info("removeHandler "+domain);
89 domain = domain.toLowerCase(); 85 domain = domain.toLowerCase();
90 synchronized(map) { 86 synchronized(map) {
91 Reference<Handler> ref = map.remove(domain); 87 Handler handler = map.remove(domain);
92 Handler handler = ref==null ? null : ref.get();
93 if( handler != null ) { 88 if( handler != null ) {
94 close(handler); 89 close(handler);
95 } 90 }
96 } 91 }
97 } 92 }