执行器"轮训"和"LRU"策略中新增缓存Clear逻辑;

pull/1/head
xueli.xue 8 years ago
parent 1c5a841579
commit 7102517093

@ -16,10 +16,18 @@ import java.util.concurrent.ConcurrentHashMap;
public class ExecutorRouteLRU extends ExecutorRouter { public class ExecutorRouteLRU extends ExecutorRouter {
private static ConcurrentHashMap<Integer, LinkedHashMap<String, String>> jobLRUMap = new ConcurrentHashMap<Integer, LinkedHashMap<String, String>>(); private static ConcurrentHashMap<Integer, LinkedHashMap<String, String>> jobLRUMap = new ConcurrentHashMap<Integer, LinkedHashMap<String, String>>();
private static long CACHE_VALID_TIME = 0;
@Override @Override
public String route(int jobId, ArrayList<String> addressList) { public String route(int jobId, ArrayList<String> addressList) {
// cache clear
if (System.currentTimeMillis() > CACHE_VALID_TIME) {
jobLRUMap.clear();
CACHE_VALID_TIME = System.currentTimeMillis() + 1000*60*60*24;
}
// init lru
LinkedHashMap<String, String> lruItem = jobLRUMap.get(jobId); LinkedHashMap<String, String> lruItem = jobLRUMap.get(jobId);
if (lruItem == null) { if (lruItem == null) {
/** /**
@ -31,12 +39,14 @@ public class ExecutorRouteLRU extends ExecutorRouter {
jobLRUMap.put(jobId, lruItem); jobLRUMap.put(jobId, lruItem);
} }
// put
for (String address: addressList) { for (String address: addressList) {
if (!lruItem.containsKey(address)) { if (!lruItem.containsKey(address)) {
lruItem.put(address, address); lruItem.put(address, address);
} }
} }
// load
String eldestKey = lruItem.entrySet().iterator().next().getKey(); String eldestKey = lruItem.entrySet().iterator().next().getKey();
String eldestValue = lruItem.get(eldestKey); String eldestValue = lruItem.get(eldestKey);
return eldestValue; return eldestValue;

@ -11,7 +11,15 @@ import java.util.concurrent.ConcurrentHashMap;
public class ExecutorRouteRound extends ExecutorRouter { public class ExecutorRouteRound extends ExecutorRouter {
private static ConcurrentHashMap<Integer, Integer> routeCountEachJob = new ConcurrentHashMap<Integer, Integer>(); private static ConcurrentHashMap<Integer, Integer> routeCountEachJob = new ConcurrentHashMap<Integer, Integer>();
private static long CACHE_VALID_TIME = 0;
private static int count(int jobId) { private static int count(int jobId) {
// cache clear
if (System.currentTimeMillis() > CACHE_VALID_TIME) {
routeCountEachJob.clear();
CACHE_VALID_TIME = System.currentTimeMillis() + 1000*60*60*24;
}
// count++
Integer count = routeCountEachJob.get(jobId); Integer count = routeCountEachJob.get(jobId);
count = (count==null)?0:++count; count = (count==null)?0:++count;
routeCountEachJob.put(jobId, count); routeCountEachJob.put(jobId, count);
@ -20,6 +28,9 @@ public class ExecutorRouteRound extends ExecutorRouter {
@Override @Override
public String route(int jobId, ArrayList<String> addressList) { public String route(int jobId, ArrayList<String> addressList) {
return addressList.get(count(jobId)%addressList.size()); return addressList.get(count(jobId)%addressList.size());
} }

Loading…
Cancel
Save