是否有一种方法可以查看cacheManager (org.springframework.cache.CacheManager)中是否存在特定的键,因为没有containsKey选项,所以我无法找到这样做的方法。如果有人能告诉我检查cacheManager中是否存在密钥,我将不胜感激。以下是我迄今尝试过的几件事-
Cache cache=null;
for (String name : cacheManager.getCacheNames()) {
cache=cacheManager.getCache(name);
System.out.println("-------------->"+cache.get("dummyCache").get()); //this will give me null pointer exception as there is no dummyCache
}我希望添加if/else检查缓存中是否存在dummyCache密钥
发布于 2020-05-19 18:25:03
您可以简单地检查Cache.get为null返回的值。
来自相关文件 (由我强调):
返回此缓存映射指定键的值,该值包含在Cache.ValueWrapper中,其中还可能包含缓存的空值。直接返回null意味着缓存不包含此键的映射。
所以
cache.get("foo") == null的意思是:缓存中不存在密钥"foo“cache.get("foo").get() == null的意思是:键"foo“确实存在于缓存中,但它的值是null。https://stackoverflow.com/questions/61897341
复制相似问题