Infinispan 缓存放置不在循环中工作
Infinispan cache put is not working in loop
我正在使用嵌入式缓存管理器,我试图将条目放入缓存中的 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");
}
}
将 infinispan-bom 添加到您的 pom.xml 文件中的启动器依赖项之前,如下所示:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.infinispan</groupId>
<artifactId>infinispan-bom</artifactId>
<version>${version.infinispan}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>${version.spring.boot}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.infinispan</groupId>
<artifactId>infinispan-spring-boot-starter</artifactId>
<version>${version.infinispan.starter}</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependency>
<groupId>org.infinispan</groupId>
<artifactId>infinispan-spring-boot-starter-embedded</artifactId>
<version>${version.infinispan.starter}</version>
</dependency>
你的豆子应该有
private final EmbeddedCacheManager cacheManager;
@Autowired
public YourBean(EmbeddedCacheManager cacheManager) {
this.cacheManager = cacheManager;
}
如果您需要自定义 EmbeddedCacheManager
做
@Bean
public InfinispanCacheConfigurer cacheConfigurer() {
return manager -> {
final Configuration ispnConfig = new ConfigurationBuilder()
.clustering()
.cacheMode(CacheMode.LOCAL)
.build();
manager.defineConfiguration("local-sync-config", ispnConfig);
};
}
您会在 https://infinispan.org/infinispan-spring-boot/master/spring_boot_starter.html
找到很好的示例
我正在使用嵌入式缓存管理器,我试图将条目放入缓存中的 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");
}
}
将 infinispan-bom 添加到您的 pom.xml 文件中的启动器依赖项之前,如下所示:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.infinispan</groupId>
<artifactId>infinispan-bom</artifactId>
<version>${version.infinispan}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>${version.spring.boot}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.infinispan</groupId>
<artifactId>infinispan-spring-boot-starter</artifactId>
<version>${version.infinispan.starter}</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependency>
<groupId>org.infinispan</groupId>
<artifactId>infinispan-spring-boot-starter-embedded</artifactId>
<version>${version.infinispan.starter}</version>
</dependency>
你的豆子应该有
private final EmbeddedCacheManager cacheManager;
@Autowired
public YourBean(EmbeddedCacheManager cacheManager) {
this.cacheManager = cacheManager;
}
如果您需要自定义 EmbeddedCacheManager
做
@Bean
public InfinispanCacheConfigurer cacheConfigurer() {
return manager -> {
final Configuration ispnConfig = new ConfigurationBuilder()
.clustering()
.cacheMode(CacheMode.LOCAL)
.build();
manager.defineConfiguration("local-sync-config", ispnConfig);
};
}
您会在 https://infinispan.org/infinispan-spring-boot/master/spring_boot_starter.html
找到很好的示例