Skip to content

Commit

Permalink
Merge pull request Meituan-Dianping#73 from Issocala/master
Browse files Browse the repository at this point in the history
优化db新加的tags灌进cache和cache中失效的tags从cache删除,利用HashSet将原来时间复杂的O(nm)变为O(n)
  • Loading branch information
Yaccc authored Mar 15, 2020
2 parents 5c409fb + eb3479d commit 26e01a2
Showing 1 changed file with 16 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,16 @@ private void updateCacheFromDb() {
return;
}
List<String> cacheTags = new ArrayList<String>(cache.keySet());
List<String> insertTags = new ArrayList<String>(dbTags);
List<String> removeTags = new ArrayList<String>(cacheTags);
Set<String> insertTagsSet = new HashSet<>(dbTags);
Set<String> removeTagsSet = new HashSet<>(cacheTags);
//db中新加的tags灌进cache
insertTags.removeAll(cacheTags);
for (String tag : insertTags) {
for(int i = 0; i < cacheTags.size(); i++){
String tmp = cacheTags.get(i);
if(insertTagsSet.contains(tmp)){
insertTagsSet.remove(tmp);
}
}
for (String tag : insertTagsSet) {
SegmentBuffer buffer = new SegmentBuffer();
buffer.setKey(tag);
Segment segment = buffer.getCurrent();
Expand All @@ -108,8 +113,13 @@ private void updateCacheFromDb() {
logger.info("Add tag {} from db to IdCache, SegmentBuffer {}", tag, buffer);
}
//cache中已失效的tags从cache删除
removeTags.removeAll(dbTags);
for (String tag : removeTags) {
for(int i = 0; i < dbTags.size(); i++){
String tmp = dbTags.get(i);
if(removeTagsSet.contains(tmp)){
removeTagsSet.remove(tmp);
}
}
for (String tag : removeTagsSet) {
cache.remove(tag);
logger.info("Remove tag {} from IdCache", tag);
}
Expand Down

0 comments on commit 26e01a2

Please sign in to comment.