|
@@ -16,10 +16,18 @@ import java.util.concurrent.ConcurrentHashMap;
|
16
|
16
|
public class ExecutorRouteLRU extends ExecutorRouter {
|
17
|
17
|
|
18
|
18
|
private static ConcurrentHashMap<Integer, LinkedHashMap<String, String>> jobLRUMap = new ConcurrentHashMap<Integer, LinkedHashMap<String, String>>();
|
|
19
|
+ private static long CACHE_VALID_TIME = 0;
|
19
|
20
|
|
20
|
21
|
@Override
|
21
|
22
|
public String route(int jobId, ArrayList<String> addressList) {
|
22
|
23
|
|
|
24
|
+ // cache clear
|
|
25
|
+ if (System.currentTimeMillis() > CACHE_VALID_TIME) {
|
|
26
|
+ jobLRUMap.clear();
|
|
27
|
+ CACHE_VALID_TIME = System.currentTimeMillis() + 1000*60*60*24;
|
|
28
|
+ }
|
|
29
|
+
|
|
30
|
+ // init lru
|
23
|
31
|
LinkedHashMap<String, String> lruItem = jobLRUMap.get(jobId);
|
24
|
32
|
if (lruItem == null) {
|
25
|
33
|
/**
|
|
@@ -31,12 +39,14 @@ public class ExecutorRouteLRU extends ExecutorRouter {
|
31
|
39
|
jobLRUMap.put(jobId, lruItem);
|
32
|
40
|
}
|
33
|
41
|
|
|
42
|
+ // put
|
34
|
43
|
for (String address: addressList) {
|
35
|
44
|
if (!lruItem.containsKey(address)) {
|
36
|
45
|
lruItem.put(address, address);
|
37
|
46
|
}
|
38
|
47
|
}
|
39
|
48
|
|
|
49
|
+ // load
|
40
|
50
|
String eldestKey = lruItem.entrySet().iterator().next().getKey();
|
41
|
51
|
String eldestValue = lruItem.get(eldestKey);
|
42
|
52
|
return eldestValue;
|