mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-05-18 21:26:35 +00:00
perf[net]: rename field
This commit is contained in:
@@ -36,7 +36,7 @@ import java.util.function.Function;
|
||||
* 1.支持批量查找,批量更新。
|
||||
* 2.可以防止缓存穿透,缓存击穿,缓存雪崩
|
||||
* <p>
|
||||
* 批量查找通过batchReloadCallback方法查找,当查找的key不存在的时候,调用defaultValueBuilder生成一个默认值放入缓存。
|
||||
* 批量查找通过batchLoadCallback方法查找,当查找的key不存在的时候,调用defaultValueBuilder生成一个默认值放入缓存。
|
||||
*
|
||||
* @author jaysunxiao
|
||||
* @version 3.0
|
||||
@@ -53,24 +53,24 @@ public class SimpleCache<K, V> {
|
||||
|
||||
private long expiredAccessDuration;
|
||||
private long refreshDuration;
|
||||
private Function<List<K>, List<Pair<K, V>>> batchReloadCallback;
|
||||
private Function<List<K>, List<Pair<K, V>>> batchLoadCallback;
|
||||
private Function<K, V> defaultValueBuilder;
|
||||
|
||||
/**
|
||||
* @param expiredAccessDuration 访问过期时间,毫秒;通常情况下,这个值比refreshDuration大会得到更好的缓存效果,一般是2倍
|
||||
* @param refreshDuration 刷新实际那,毫秒;因为是通过后台scheduler去更新缓存,所以更新的refresh可能是这个值的2倍
|
||||
* @param maxSize 缓存大小
|
||||
* @param batchReloadCallback 一组key取value的回调方法
|
||||
* @param batchLoadCallback 一组key取value的回调方法
|
||||
* @param defaultValueBuilder 默认值构建
|
||||
* @return 简单的缓存
|
||||
*/
|
||||
public static <K, V> SimpleCache<K, V> build(long expiredAccessDuration, long refreshDuration, long maxSize
|
||||
, Function<List<K>, List<Pair<K, V>>> batchReloadCallback
|
||||
, Function<List<K>, List<Pair<K, V>>> batchLoadCallback
|
||||
, Function<K, V> defaultValueBuilder) {
|
||||
|
||||
var linkedQueue = new ConcurrentLinkedQueue<K>();
|
||||
|
||||
// 没有用guava的expireAfterWrite的原因是容易造成缓存击穿
|
||||
// 没有用expireAfterWrite的原因是容易造成缓存击穿
|
||||
var cache = Caffeine.newBuilder()
|
||||
.expireAfterAccess(expiredAccessDuration, TimeUnit.MILLISECONDS)
|
||||
.refreshAfterWrite(refreshDuration, TimeUnit.MILLISECONDS)
|
||||
@@ -79,15 +79,15 @@ public class SimpleCache<K, V> {
|
||||
.build(new CacheLoader<K, V>() {
|
||||
@Override
|
||||
public @Nullable V load(@NonNull K key) {
|
||||
// 因为通过SimpleCache封装过后,上层逻辑是不会调用guava的cache的get方法,所以理论上load不会执行
|
||||
var resultList = batchReloadCallback.apply(List.of(key));
|
||||
var resultList = batchLoadCallback.apply(List.of(key));
|
||||
return CollectionUtils.isEmpty(resultList) ? defaultValueBuilder.apply(key) : resultList.get(0).getValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable V reload(@NonNull K key, @NonNull V oldValue) {
|
||||
// 将待刷新的缓存放入队列,等Scheduler周期任务批量刷新老的缓存值
|
||||
linkedQueue.offer(key);
|
||||
// 先返回老的值,等周期任务刷新新的值
|
||||
// 先返回老的值
|
||||
return oldValue;
|
||||
}
|
||||
});
|
||||
@@ -105,14 +105,14 @@ public class SimpleCache<K, V> {
|
||||
var key = linkedQueue.poll();
|
||||
list.add(key);
|
||||
if (list.size() >= BATCH_RELOAD_SIZE) {
|
||||
var result = batchReloadCallback.apply(list);
|
||||
var result = batchLoadCallback.apply(list);
|
||||
result.forEach(it -> cache.put(it.getKey(), it.getValue()));
|
||||
list.clear();
|
||||
}
|
||||
}
|
||||
|
||||
if (CollectionUtils.isNotEmpty(list)) {
|
||||
var result = batchReloadCallback.apply(list);
|
||||
var result = batchLoadCallback.apply(list);
|
||||
result.forEach(it -> cache.put(it.getKey(), it.getValue()));
|
||||
}
|
||||
}
|
||||
@@ -126,7 +126,7 @@ public class SimpleCache<K, V> {
|
||||
simpleCache.linkedQueue = linkedQueue;
|
||||
simpleCache.expiredAccessDuration = expiredAccessDuration;
|
||||
simpleCache.refreshDuration = refreshDuration;
|
||||
simpleCache.batchReloadCallback = batchReloadCallback;
|
||||
simpleCache.batchLoadCallback = batchLoadCallback;
|
||||
simpleCache.defaultValueBuilder = defaultValueBuilder;
|
||||
return simpleCache;
|
||||
}
|
||||
@@ -175,7 +175,7 @@ public class SimpleCache<K, V> {
|
||||
}
|
||||
|
||||
if (CollectionUtils.isNotEmpty(notPresentIdSet)) {
|
||||
batchReloadCallback.apply(new ArrayList<>(notPresentIdSet))
|
||||
batchLoadCallback.apply(new ArrayList<>(notPresentIdSet))
|
||||
.forEach(it -> {
|
||||
result.put(it.getKey(), it.getValue());
|
||||
cache.put(it.getKey(), it.getValue());
|
||||
|
||||
Reference in New Issue
Block a user