이 콘텐츠는 선택한 언어로 제공되지 않습니다.
Chapter 2. Using Embedded Caches
Embed Data Grid caches directly in your project for in-memory data storage.
2.1. Adding the EmbeddedCacheManager Bean 링크 복사링크가 클립보드에 복사되었습니다!
Configure your application to use embedded caches.
Procedure
-
Add
infinispan-spring-boot-starter-embeddedto your project’s classpath to enable Embedded mode. Use the Spring
@Autowiredannotation to include anEmbeddedCacheManagerbean in your Java configuration classes, as in the following example:private final EmbeddedCacheManager cacheManager; @Autowired public YourClassName(EmbeddedCacheManager cacheManager) { this.cacheManager = cacheManager; }
You are now ready to use Data Grid caches directly within your application, as in the following example:
cacheManager.getCache("testCache").put("testKey", "testValue");
System.out.println("Received value from cache: " + cacheManager.getCache("testCache").get("testKey"));
2.2. Cache Manager Configuration Beans 링크 복사링크가 클립보드에 복사되었습니다!
You can customize the cache manager with the following configuration beans:
-
InfinispanGlobalConfigurer -
InfinispanCacheConfigurer -
Configuration -
InfinispanConfigurationCustomizer -
InfinispanGlobalConfigurationCustomizer
You can create one InfinispanGlobalConfigurer bean only. However you can create multiple configurations with the other beans.
InfinispanCacheConfigurer Bean
@Bean
public InfinispanCacheConfigurer cacheConfigurer() {
return manager -> {
final Configuration ispnConfig = new ConfigurationBuilder()
.clustering()
.cacheMode(CacheMode.LOCAL)
.build();
manager.defineConfiguration("local-sync-config", ispnConfig);
};
}
Configuration Bean
Link the bean name to the cache that it configures, as follows:
@Bean(name = "small-cache")
public org.infinispan.configuration.cache.Configuration smallCache() {
return new ConfigurationBuilder()
.read(baseCache)
.memory().size(1000L)
.memory().evictionType(EvictionType.COUNT)
.build();
}
@Bean(name = "large-cache")
public org.infinispan.configuration.cache.Configuration largeCache() {
return new ConfigurationBuilder()
.read(baseCache)
.memory().size(2000L)
.build();
}
Customizer Beans
@Bean
public InfinispanGlobalConfigurationCustomizer globalCustomizer() {
return builder -> builder.transport().clusterName(CLUSTER_NAME);
}
@Bean
public InfinispanConfigurationCustomizer configurationCustomizer() {
return builder -> builder.memory().evictionType(EvictionType.COUNT);
}
2.3. Enabling Spring Cache Support 링크 복사링크가 클립보드에 복사되었습니다!
With both embedded and remote caches, Data Grid provides an implementation of Spring Cache that you can enable.
Procedure
-
Add the
@EnableCachingannotation to your application.
If the Data Grid starter detects the:
-
EmbeddedCacheManagerbean, it instantiates a newSpringEmbeddedCacheManager. -
RemoteCacheManagerbean, it instantiates a newSpringRemoteCacheManager.
Reference