Skip to content

Commit

Permalink
del[cache]: delete sizeCheckTimeAtomic
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysunxiao committed Mar 27, 2024
1 parent 7fcf726 commit 03d7996
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 17 deletions.
22 changes: 6 additions & 16 deletions scheduler/src/main/java/com/zfoo/scheduler/util/LazyCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
public class LazyCache<K, V> {

private static final float DEFAULT_BACK_PRESSURE_FACTOR = 0.11f;
private static final long MILLIS_MAX_SIZE_CHECK_INTERVAL = 3;

private static class CacheValue<V> {
public volatile V value;
Expand Down Expand Up @@ -58,7 +57,6 @@ public static enum RemovalCause {
private long expireCheckIntervalMillis;
private volatile long minExpireTime;
private AtomicLong expireCheckTimeAtomic;
private AtomicLong sizeCheckTimeAtomic;
private ConcurrentMap<K, CacheValue<V>> cacheMap;
private BiConsumer<Pair<K, V>, RemovalCause> removeListener = (pair, removalCause) -> {
};
Expand All @@ -70,7 +68,6 @@ public LazyCache(int maximumSize, long expireAfterAccessMillis, long expireCheck
this.expireCheckIntervalMillis = expireCheckIntervalMillis;
this.minExpireTime = TimeUtils.now();
this.expireCheckTimeAtomic = new AtomicLong(TimeUtils.now() + expireCheckIntervalMillis);
this.sizeCheckTimeAtomic = new AtomicLong(TimeUtils.now());
this.cacheMap = new ConcurrentHashMap<>(Math.max(maximumSize / 16, 512));
if (removeListener != null) {
this.removeListener = removeListener;
Expand Down Expand Up @@ -136,19 +133,12 @@ public int size() {
// -----------------------------------------------------------------------------------------------------------------
private void checkMaximumSize() {
if (cacheMap.size() > backPressureSize) {
var currentTimeMillis = TimeUtils.currentTimeMillis();
var sizeCheckTime = sizeCheckTimeAtomic.get();
if (currentTimeMillis > sizeCheckTime) {
if (sizeCheckTimeAtomic.compareAndSet(sizeCheckTime, currentTimeMillis + MILLIS_MAX_SIZE_CHECK_INTERVAL)) {
var exceedList = cacheMap.entrySet()
.stream()
.map(it -> new PairLong<>(it.getValue().expireTime, it.getKey()))
.sorted((a, b) -> Long.compare(a.getKey(), b.getKey()))
.limit(Math.max(0, cacheMap.size() - maximumSize))
.toList();
exceedList.forEach(it -> removeForCause(it.getValue(), RemovalCause.SIZE));
}
}
cacheMap.entrySet()
.stream()
.map(it -> new PairLong<>(it.getValue().expireTime, it.getKey()))
.sorted((a, b) -> Long.compare(a.getKey(), b.getKey()))
.limit(Math.max(0, cacheMap.size() - maximumSize))
.forEach(it -> removeForCause(it.getValue(), RemovalCause.SIZE));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,10 @@ public void run() {
}
});
}
ThreadUtils.sleep(Long.MAX_VALUE);
for (int i = 0; i < 10000; i++) {
logger.info("cache size:[{}]", lazyCache.size());
ThreadUtils.sleep(1000);
}
}

}

0 comments on commit 03d7996

Please sign in to comment.