- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有缓存,每天从数据库加载所有客户详细信息。但在加载每日客户详细信息之前,我需要删除缓存中以前的所有条目。
目前我正在做:
public enum PeriodicUpdater {
TIMER;
private final AtomicBoolean isPublishing = new AtomicBoolean(false);
private final long period = TimeUnit.DAYS.toMillis(1);
@Autowired
@Qualifier("TestUtils") @Setter
private TestUtils testUtils;
public synchronized boolean initialize() {
return initialize(period, period);
}
boolean initialize(long delay, long period) {
if (isPublishing.get()) {
return false;
}
TimerTask task = new TimerTask() {
@Override public void run() {
try {
String path = getFile();
if(TestUtils.getFileNameCache().getIfPresent(path) == null) {
TestUtils.setFileNameCache(testUtils.buildFileCache(path));
}
} catch (Exception e) {
log.warn("Failed!", e);
}
}
};
Timer timer = new Timer("PeriodicUpdater", true); // daemon=true
timer.schedule(task, delay, period);
isPublishing.set(true);
return true;
}
}
我在这里使用缓存:
public class TestUtils {
private static Cache<String, Map<String, List<String>>> fileCache = CacheBuilder
.newBuilder()
.expireAfterWrite(4, TimeUnit.DAYS)
.build();
public TestUtils() {
String path = getFile();
fileNameCache = buildFileCache(path);
}
public Cache<String, String> buildFileCache(String path) {
Cache<String, String> fileList = CacheBuilder
.newBuilder()
.expireAfterWrite(4, TimeUnit.DAYS)
.build();
fileList.put(path, new Date().toString());
return fileList;
}
/* doing some stuff with the cache */
}
这样做正确吗?我没有看到缓存被清除。如果我错了,有人可以纠正我吗?
最佳答案
Cache.invalidateAll()
将清除当前缓存中的所有条目。
也就是说,如果您打算每天重新加载条目,为什么只每四天使缓存内容过期(.expireAfterWrite(4, TimeUnit.DAYS)
)?只需将 4
更改为 1
即可每天重新加载内容一次。
此外,正如 Adrian Shum 提到的,您滥用了枚举。 public enum periodicupdater
几乎肯定应该是public class periodicupdater
。
关于Java Guava 缓存: How to clear all cache entries?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42150174/
这个问题不太可能帮助任何 future 的访客;它只与一个小地理区域、一个特定时刻或一个非常狭窄的情况相关,而这些情况通常不适用于互联网的全局受众。如需帮助使这个问题更广泛地适用,visit the
我有: data class Edge(val spec: String, val weight: Int) private val graph: SortedSetMultimap = TreeMu
鉴于使用以下代码创建的 Guava 缓存,如果未设置,是否有最大缓存大小? LoadingCache loadingCache = CacheBuilder.newBuilder().build(ne
我需要向 Guava Multimap 添加一个键,其中一个空集合作为值。我该如何做到这一点? 我试过这个: map.put( "my key", null ); 但是调用 get() 会返回一个包含
我刚刚遇到这样的代码: ExecutorService executorService = MoreExecutors.sameThreadExecutor(); for (int i = 0; i
我使用的是来自 Google Collections 的 com.google.common.collect.PrimitiveArrays,但是我在 Guava 中找不到它,是否已重命名?我在哪里可
当前,我正在使用以下代码在映射中创建过滤器以匹配并提供过滤后的结果集列表。 final Map filteredMap = Maps.filterKeys(mymap, Predicates.cont
当我在 app/build.gradle 中使用 implementation 'com.google.firebase:firebase-inappmessaging-display:17.2.0'
Google Guava Cache 文档指出: Refreshing is not quite the same as eviction. As specified in LoadingCache.
Guava 的 ImmutableList.Builder 的线程安全保证是什么? javadocs 没有说。 最佳答案 虽然 Guava Immutable 类是线程安全的,但它们的构建器不是。对于
目前我在我的应用程序中使用 guava EventBus 方法。监听器尝试做一些工作,如果失败,事件应该回到总线并重新发送。 我的问题是:如果我的应用程序出现故障(执行关闭)怎么办?它会在总线上发送剩
是否可以使用现有的 java 静态方法作为开箱即用的扩展? 让我们考虑 com.google.common.collect.Iterables.transform。现在,因为我不知道如何处理这个问题,
我想创建一个由 Guava 函数支持的只读 map 。我有一个提供值的函数,给定一个键。 Function f = new Function() { public Object apply(f
我最近将 Google Guava 作为库添加到我的 Eclipse 项目中(我从 http://code.google.com/p/guava-libraries/ 下载了“guava-16.0.j
我们最近从 Drools 5 升级到 Drools 6 并遇到了令人不安的冲突问题。 我们有kie-ci导入到项目中。 kie-ci引进 sisu-guava . sisu-guava改变了谷歌 Gu
尝试取消注册时,我在我的一个类(class)中收到以下错误。 java.lang.IllegalArgumentException: missing event handler for an anno
我的项目传递依赖于 Google Guava lib。突然(使用新版本的 Guava ?)应用程序在启动时崩溃java.lang.NoSuchMethodError: 'java.util.strea
我喜欢 Google Guava 并且经常使用它,但是我总是发现我在写一种方法。 public static T tryFind(Iterable iterable, Predicate pred
我使用的是普通的旧 Java 1.6,并且对这两个库都感兴趣。 阅读文档后,我不确定是否存在差异(如果有的话)。任何人都可以解释一下,或者指出一些相关信息吗?提前致谢! 最佳答案 RxJava 比 L
我用的是 Guava 17.0 private static final ConcurrentMap imageMap = new MapMaker().softValues().ma
我是一名优秀的程序员,十分优秀!