此内容没有您所选择的语言版本。
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-boot3-starter-embedded
to your project’s classpath to enable Embedded mode. Use the Spring
@Autowired
annotation to include anEmbeddedCacheManager
bean in your Java configuration classes, as in the following example:Copy to Clipboard Copied! Toggle word wrap Toggle overflow
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"));
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
Configuration Bean
Link the bean name to the cache that it configures, as follows:
Customizer Beans
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
@EnableCaching
annotation to your application.
If the Data Grid starter detects the:
-
EmbeddedCacheManager
bean, it instantiates a newSpringEmbeddedCacheManager
. -
RemoteCacheManager
bean, it instantiates a newSpringRemoteCacheManager
.
Reference