Skip to content

Commit

Permalink
fix[LazyCache]: ConcurrentHashMap unsafe remove in for
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysunxiao committed Mar 25, 2024
1 parent 5ca7731 commit c701dd8
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion orm/src/main/java/com/zfoo/orm/util/LazyCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.zfoo.protocol.model.Pair;
import com.zfoo.scheduler.util.TimeUtils;

import java.util.ArrayList;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicLong;
Expand Down Expand Up @@ -149,15 +150,17 @@ private void checkExpire() {
if (expireCheckTimeAtomic.compareAndSet(expireCheckTime, now + expireCheckInterval)) {
if (now > this.minExpireTime) {
var minTimestamp = Long.MAX_VALUE;
var removeList = new ArrayList<K>();
for (var entry : cacheMap.entrySet()) {
var expireTime = entry.getValue().expireTime;
if (expireTime < now) {
remove(entry.getKey(), RemovalCause.EXPIRED);
removeList.add(entry.getKey());
}
if (expireTime < minTimestamp) {
minTimestamp = expireTime;
}
}
removeList.forEach(it -> remove(it, RemovalCause.EXPIRED));
this.minExpireTime = minTimestamp;
}
}
Expand Down

0 comments on commit c701dd8

Please sign in to comment.