Chapter 1. Using Embedded Caches
Embed Data Grid caches directly in your project for in-memory data storage.
1.1. Adding the EmbeddedCacheManager Bean Copy linkLink copied to clipboard!
Configure your application to use embedded caches.
Procedure
-
Add
infinispan-spring-boot3-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: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"));
1.2. Using the reactive mode with Reactor Copy linkLink copied to clipboard!
Starting with Spring 6.1, reactive mode is supported to make use of caching within reactive applications. If you use spring-boot-starter-webflux, your application may block.
To enable the Data Grid reactive driver, specify the following property in application.properties:
infinispan.embedded.reactive=true
infinispan.embedded.reactive=true
1.3. Cache Manager Configuration Beans Copy linkLink copied to clipboard!
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
1.4. Enabling Spring Cache Support Copy linkLink copied to clipboard!
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