执行器"轮训"和"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 {
private static ConcurrentHashMap<Integer, LinkedHashMap<String, String>> jobLRUMap = new ConcurrentHashMap<Integer, LinkedHashMap<String, String>>();
private static long CACHE_VALID_TIME = 0;
@Override
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);
if (lruItem == null) {
/**
@ -31,12 +39,14 @@ public class ExecutorRouteLRU extends ExecutorRouter {
jobLRUMap.put(jobId, lruItem);
}
// put
for (String address: addressList) {
if (!lruItem.containsKey(address)) {
lruItem.put(address, address);
}
}
// load
String eldestKey = lruItem.entrySet().iterator().next().getKey();
String eldestValue = lruItem.get(eldestKey);
return eldestValue;

@ -11,7 +11,15 @@ import java.util.concurrent.ConcurrentHashMap;
public class ExecutorRouteRound extends ExecutorRouter {
private static ConcurrentHashMap<Integer, Integer> routeCountEachJob = new ConcurrentHashMap<Integer, Integer>();
private static long CACHE_VALID_TIME = 0;
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);
count = (count==null)?0:++count;
routeCountEachJob.put(jobId, count);
@ -20,6 +28,9 @@ public class ExecutorRouteRound extends ExecutorRouter {
@Override
public String route(int jobId, ArrayList<String> addressList) {
return addressList.get(count(jobId)%addressList.size());
}

Loading…
Cancel
Save