【发布时间】:2020-09-08 20:38:35
【问题描述】:
我正在使用嵌入式缓存管理器,并且我正在尝试将条目放入 for 循环中的缓存中,但是当下一个请求被触发时,我看到缓存为空。下面是我的实现-
EmbeddedCacheManager manager=null;
{
try {
manager = new DefaultCacheManager("src/main/resources/infinispan.xml");
} catch (IOException e) {
e.printStackTrace();
}
}
Cache cache=manager.getCache();
for (JsonElement jsonElement : jsonArray)
{
id=jsonElement.getAsJsonObject().get("_id").getAsString();
id1= id.replace("-","");
JsonObject source = jsonElement.getAsJsonObject().getAsJsonObject("_src");
String jsonString = source.toString();
Dummy dummy = new Gson().fromJson(jsonString, Dummy.class);
if(cache.get(id1)!=null){
retVal.add((Dummy) cache.get(id1));
System.out.println("This is cached !");
}else {
cache.put(id1,dummy);
retVal.add(dummy);
System.out.println("This is not cached");
}
}
【问题讨论】:
-
由于该代码不完整很难说,但看起来您正在为每个请求创建一次缓存管理器。你检查过日志吗?
-
@RadimVansa 是的,这就是我在日志中看到的。每个请求都在创建缓存管理器。我该如何避免这种情况?有没有我可以遵循的示例,我不为每个请求创建缓存管理器
标签: spring-boot caching infinispan