mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-10-11 03:13:51 +00:00
feat[cache]: add refresh cache method to SingleCache
This commit is contained in:
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());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user