mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-10-12 22:15:02 +00:00
perf[cache]: assert construct param
This commit is contained in:
1 parent
faace1de6a
commit
b3e61091a2
2 files changed
+65
-2
No files matched your search
@@ -2,6 +2,7 @@ package com.zfoo.scheduler.util;
|
||||
|
||||
import com.zfoo.protocol.model.Pair;
|
||||
import com.zfoo.protocol.model.PairLong;
|
||||
import com.zfoo.protocol.util.AssertionUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
@@ -62,12 +63,15 @@ public class LazyCache<K, V> {
|
||||
};
|
||||
|
||||
public LazyCache(int maximumSize, long expireAfterAccessMillis, long expireCheckIntervalMillis, BiConsumer<Pair<K, V>, RemovalCause> removeListener) {
|
||||
AssertionUtils.ge1(maximumSize);
|
||||
AssertionUtils.ge0(expireAfterAccessMillis);
|
||||
AssertionUtils.ge0(expireCheckIntervalMillis);
|
||||
this.maximumSize = maximumSize;
|
||||
this.backPressureSize = Math.max(maximumSize, maximumSize + (int) (maximumSize * DEFAULT_BACK_PRESSURE_FACTOR));
|
||||
this.expireAfterAccessMillis = expireAfterAccessMillis;
|
||||
this.expireCheckIntervalMillis = expireCheckIntervalMillis;
|
||||
this.expireCheckIntervalMillis = Math.max(1, expireCheckIntervalMillis);
|
||||
this.minExpireTime = TimeUtils.now();
|
||||
this.expireCheckTimeAtomic = new AtomicLong(TimeUtils.now() + expireCheckIntervalMillis);
|
||||
this.expireCheckTimeAtomic = new AtomicLong(TimeUtils.now());
|
||||
this.cacheMap = new ConcurrentHashMap<>(Math.max(maximumSize / 16, 512));
|
||||
if (removeListener != null) {
|
||||
this.removeListener = removeListener;
|
||||
|
||||
@@ -129,6 +129,65 @@ public class LazyCacheTest {
|
||||
}
|
||||
}
|
||||
|
||||
// check interval test
|
||||
@Test
|
||||
public void multipleThreadCheckIntervalTest() {
|
||||
int threadNum = Runtime.getRuntime().availableProcessors() + 1;
|
||||
ExecutorService[] executors = new ExecutorService[threadNum];
|
||||
for (int i = 0; i < executors.length; i++) {
|
||||
executors[i] = Executors.newSingleThreadExecutor();
|
||||
}
|
||||
var lazyCache = new LazyCache<Integer, String>(1_0000, 10000000 * TimeUtils.MILLIS_PER_SECOND, 0, myRemoveCallback);
|
||||
for (int i = 0; i < executors.length; i++) {
|
||||
|
||||
var executor = executors[i];
|
||||
int i1 = i;
|
||||
executor.execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
var startIndex = i1 * 1_0000;
|
||||
for (int j = i1 * 1_0000; j < startIndex + 1_0000; j++) {
|
||||
lazyCache.put(j, String.valueOf(j));
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
for (int i = 0; i < 10000; i++) {
|
||||
logger.info("cache size:[{}]", lazyCache.size());
|
||||
ThreadUtils.sleep(1000);
|
||||
}
|
||||
}
|
||||
|
||||
// expire after access test
|
||||
@Test
|
||||
public void multipleThreadExpireAfterAccessTest() {
|
||||
int threadNum = Runtime.getRuntime().availableProcessors() + 1;
|
||||
ExecutorService[] executors = new ExecutorService[threadNum];
|
||||
for (int i = 0; i < executors.length; i++) {
|
||||
executors[i] = Executors.newSingleThreadExecutor();
|
||||
}
|
||||
var lazyCache = new LazyCache<Integer, String>(1_0000, 0, 0, null);
|
||||
for (int i = 0; i < executors.length; i++) {
|
||||
|
||||
var executor = executors[i];
|
||||
int i1 = i;
|
||||
executor.execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
var startIndex = i1 * 1_0000;
|
||||
for (int j = i1 * 1_0000; j < startIndex + 1_0000; j++) {
|
||||
lazyCache.put(j, String.valueOf(j));
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
for (int i = 0; i < 10000; i++) {
|
||||
lazyCache.get(-1);
|
||||
logger.info("cache size:[{}]", lazyCache.size());
|
||||
ThreadUtils.sleep(100);
|
||||
}
|
||||
}
|
||||
|
||||
// not expired test
|
||||
@Test
|
||||
public void multipleThreadNotExpiredTest() {
|
||||
|
||||
Reference in new issue
Block a user