Using Data Grid with Spring
Add Data Grid to Spring applications
Abstract
Red Hat Data Grid Copy linkLink copied to clipboard!
Data Grid is a high-performance, distributed in-memory data store.
- Schemaless data structure
- Flexibility to store different objects as key-value pairs.
- Grid-based data storage
- Designed to distribute and replicate data across clusters.
- Elastic scaling
- Dynamically adjust the number of nodes to meet demand without service disruption.
- Data interoperability
- Store, retrieve, and query data in the grid from different endpoints.
Data Grid documentation Copy linkLink copied to clipboard!
Documentation for Data Grid is available on the Red Hat customer portal.
Data Grid downloads Copy linkLink copied to clipboard!
Access the Data Grid Software Downloads on the Red Hat customer portal.
You must have a Red Hat account to access and download Data Grid software.
Making open source more inclusive Copy linkLink copied to clipboard!
Red Hat is committed to replacing problematic language in our code, documentation, and web properties. We are beginning with these four terms: master, slave, blacklist, and whitelist. Because of the enormity of this endeavor, these changes will be implemented gradually over several upcoming releases. For more details, see our CTO Chris Wright’s message.
Chapter 1. Using Data Grid as a Spring Cache provider Copy linkLink copied to clipboard!
Add Data Grid dependencies to your application and use Spring Cache annotations to store data in embedded or remote caches.
1.1. Setting up Spring caching with Data Grid Copy linkLink copied to clipboard!
Add the Data Grid dependencies to your Spring application project. If you use remote caches in a Data Grid Server deployment, you should also configure your Hot Rod client properties.
Data Grid only supports Spring version version 6.
Procedure
Add Data Grid and the Spring integration module to your
pom.xml.-
Remote caches:
infinispan-spring6-remote Embedded caches:
infinispan-spring6-embeddedTipSpring Boot users can add the following artifacts instead of the
infinispan-spring6-embedded:-
For Spring Boot 3 add
infinispan-spring-boot3-starter-embedded
-
For Spring Boot 3 add
-
Remote caches:
Configure your Hot Rod client to connect to your Data Grid Server deployment in the
hotrod-client.propertiesfile.infinispan.client.hotrod.server_list = 127.0.0.1:11222 infinispan.client.hotrod.auth_username=admin infinispan.client.hotrod.auth_password=changeme
infinispan.client.hotrod.server_list = 127.0.0.1:11222 infinispan.client.hotrod.auth_username=admin infinispan.client.hotrod.auth_password=changemeCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Spring Cache dependencies
Remote caches
Embedded caches
1.2. Using Data Grid as a Spring Cache provider Copy linkLink copied to clipboard!
Add the @EnableCaching annotation to one of your configuration classes and then add the @Cacheable and @CacheEvict annotations to use remote or embedded caches.
Prerequisites
- Add the Data Grid dependencies to your application project.
- Create the required remote caches and configure Hot Rod client properties if you use a Data Grid Server deployment.
Procedure
Enable cache annotations in your application context in one of the following ways:
Declarative
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Programmatic
@EnableCaching @Configuration public class Config { }@EnableCaching @Configuration public class Config { }Copy to Clipboard Copied! Toggle word wrap Toggle overflow Annotate methods with
@Cacheableto cache return values.TipTo reference entries in the cache directly, you must include the
keyattribute.-
Annotate methods with
@CacheEvictto remove old entries from the cache.
1.3. Spring Cache annotations Copy linkLink copied to clipboard!
The @Cacheable and @CacheEvict annotations add cache capabilities to methods.
@Cacheable- Stores return values in a cache.
@CacheEvict- Controls cache size by removing old entries.
@Cacheable
Taking Book objects as an example, if you want to cache each instance after loading it from a database with a method such as BookDao#findBook(Integer bookId), you could add the @Cacheable annotation as follows:
@Transactional
@Cacheable(value = "books", key = "#bookId")
public Book findBook(Integer bookId) {...}
@Transactional
@Cacheable(value = "books", key = "#bookId")
public Book findBook(Integer bookId) {...}
With the preceding example, when findBook(Integer bookId) returns a Book instance it gets stored in the cache named books.
@CacheEvict
With the @CacheEvict annotation, you can specify if you want to evict the entire books cache or only the entries that match a specific #bookId.
Entire cache eviction
Annotate the deleteAllBookEntries() method with @CacheEvict and add the allEntries parameter as follows:
@Transactional
@CacheEvict (value="books", key = "#bookId", allEntries = true)
public void deleteAllBookEntries() {...}
@Transactional
@CacheEvict (value="books", key = "#bookId", allEntries = true)
public void deleteAllBookEntries() {...}
Entry based eviction
Annotate the deleteBook(Integer bookId) method with @CacheEvict and specify the key associated to the entry as follows:
@Transactional
@CacheEvict (value="books", key = "#bookId")
public void deleteBook(Integer bookId) {...}
@Transactional
@CacheEvict (value="books", key = "#bookId")
public void deleteBook(Integer bookId) {...}
1.4. Configuring timeouts for cache operations Copy linkLink copied to clipboard!
The Data Grid Spring Cache provider defaults to blocking behaviour when performing read and write operations. Cache operations are synchronous and do not time out.
If necessary you can configure a maximum time to wait for operations to complete before they time out.
Procedure
Configure the following timeout properties in the context XML for your application on either
SpringEmbeddedCacheManagerFactoryBeanorSpringRemoteCacheManagerFactoryBean.For remote caches, you can also add these properties to the
hotrod-client.propertiesfile.
| Property | Description |
|---|---|
|
|
Specifies the time, in milliseconds, to wait for read operations to complete. The default is |
|
|
Specifies the time, in milliseconds, to wait for write operations to complete. The default is |
The following example shows the timeout properties in the context XML for SpringRemoteCacheManagerFactoryBean:
Chapter 2. Externalizing sessions with Spring Session Copy linkLink copied to clipboard!
Store session data for Spring applications in Data Grid caches and independently of the container.
2.1. Externalizing Sessions with Spring Session Copy linkLink copied to clipboard!
Use the Spring Session API to externalize session data to Data Grid.
Procedure
Add dependencies to your
pom.xml.-
Embedded caches:
infinispan-spring6-embedded Remote caches:
infinispan-spring6-remoteThe following example is for remote caches:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
-
Embedded caches:
Specify the appropriate
FactoryBeanto expose aCacheManagerinstance.-
Embedded caches:
SpringEmbeddedCacheManagerFactoryBean -
Remote caches:
SpringRemoteCacheManagerFactoryBean
-
Embedded caches:
Enable Spring Session with the appropriate annotation.
-
Embedded caches:
@EnableInfinispanEmbeddedHttpSession Remote caches:
@EnableInfinispanRemoteHttpSessionThese annotations have optional parameters:
-
maxInactiveIntervalInSecondssets session expiration time in seconds. The default is1800. -
cacheNamespecifies the name of the cache that stores sessions. The default issessions.
-
-
Embedded caches:
The following example shows a complete, annotation-based configuration: