feat[cache]: add refresh cache method to SingleCache

This commit is contained in:
jaysunxiao committed 2025-07-06 15:33:49 +08:00
1 parent 62b2b6c5d9
commit c2cb27c535
1 file changed
+6 -2
@@ -53,7 +53,7 @@ public class SingleCache<V> {
public static <V> SingleCache<V> lazyBuild(long refreshDuration, Supplier<V> supplier) {
var cache = new SingleCache<V>(refreshDuration, supplier);
EventBus.asyncExecute(cache.hashCode(), () -> cache.cache = supplier.get());
cache.lazyRefresh();
return cache;
}
@@ -76,7 +76,7 @@ public class SingleCache<V> {
// 使用双重检测锁的方式
if (now > refreshTime) {
if (refreshTimeAtomic.compareAndSet(refreshTime, now + refreshDuration)) {
EventBus.asyncExecute(cache.hashCode(), () -> cache = supplier.get());
lazyRefresh();
}
}
return cache;
@@ -86,4 +86,8 @@ public class SingleCache<V> {
cache = value;
}
public void lazyRefresh() {
EventBus.asyncExecute(cache.hashCode(), () -> cache = supplier.get());
}
}