如何查找cacheManager(org.springframework.cache.CacheManager)中是否存在key。

问题描述 投票:0回答:1

有没有办法查看cacheManager中是否存在某个特定的key?org.springframework.cache.CacheManager),因为我无法找到这样的方法,因为没有任何的 containsKey 选项。如果有人能告诉我一个方法来检查cacheManager中是否存在一个key,我会很感激。以下是我目前尝试过的一些方法------。

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 
    }

我想添加ifelse检查,以查看是否有一个 dummyCache缓存键

spring-boot caching infinispan
1个回答
1
投票

你可以简单地检查 Cache.get 对于 null.

来自 相关文件 (我强调的)。

返回该缓存对指定键的映射值,该值包含在Cache.ValueWrapper中,该Cache.ValueWrapper也可能包含一个缓存的空值。直接返回空值意味着缓存中不包含这个键的映射。

所以

  • cache.get("foo") == null 意思是:"foo "这个键在缓存中不存在。缓存中没有 "foo "这个键。
  • cache.get("foo").get() == null 意思是说:"foo "这个键在缓存中确实存在,但它的值是 缓存中确实存在 "foo "这个键,但它的值是: null
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.