Skip to content

Commit

Permalink
perf[cache]: check expire cache in all access method
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysunxiao committed Aug 16, 2024
1 parent 96485d1 commit d676bc4
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions scheduler/src/main/java/com/zfoo/scheduler/util/LazyCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,24 @@ public V get(K key) {


public void remove(K key) {
checkExpire();
removeForCause(key, RemovalCause.EXPLICIT);
}

public void forEach(BiConsumer<K, V> biConsumer) {
checkExpire();
for (var entry : cacheMap.entrySet()) {
biConsumer.accept(entry.getKey(), entry.getValue().value);
}
}

public int size() {
checkExpire();
return cacheMap.size();
}


// -----------------------------------------------------------------------------------------------------------------
private void removeForCause(K key, RemovalCause removalCause) {
if (key == null) {
return;
Expand All @@ -134,18 +149,6 @@ private void removeForCause(List<Pair<K, V>> list, RemovalCause removalCause) {
removeListener.accept(removeList, removalCause);
}

public void forEach(BiConsumer<K, V> biConsumer) {
for (var entry : cacheMap.entrySet()) {
biConsumer.accept(entry.getKey(), entry.getValue().value);
}
}

public int size() {
return cacheMap.size();
}


// -----------------------------------------------------------------------------------------------------------------
private void checkMaximumSize() {
if (cacheMap.size() > backPressureSize) {
var removeList = cacheMap.entrySet()
Expand Down

0 comments on commit d676bc4

Please sign in to comment.