Chapter 1. Using Data Grid as a Spring Cache provider

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

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 supports Spring version 5 and version 6. Be aware that Spring 6 requires Java 17.

The examples in this document include artifacts for the latest version of Spring. If you want to use Spring 5 use:

  • Remote caches: infinispan-spring5-remote
  • Embedded caches: infinispan-spring5-embedded


  1. Add Data Grid and the Spring integration module to your pom.xml.

    • Remote caches: infinispan-spring6-remote
    • Embedded caches: infinispan-spring6-embedded


      Spring 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 2.x add infinispan-spring-boot-starter-embedded
  2. Configure your Hot Rod client to connect to your Data Grid Server deployment in the file.

    infinispan.client.hotrod.server_list =

Spring Cache dependencies

Remote caches


Embedded caches


1.2. Using Data Grid as a Spring Cache provider

Add the @EnableCaching annotation to one of your configuration classes and then add the @Cacheable and @CacheEvict annotations to use remote or embedded caches.


  • 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.


  1. Enable cache annotations in your application context in one of the following ways:


    <beans xmlns=""
            <cache:annotation-driven />


    @EnableCaching @Configuration
    public class Config {

  2. Annotate methods with @Cacheable to cache return values.


    To reference entries in the cache directly, you must include the key attribute.

  3. Annotate methods with @CacheEvict to remove old entries from the cache.

1.3. Spring Cache annotations

The @Cacheable and @CacheEvict annotations add cache capabilities to methods.

Stores return values in a cache.
Controls cache size by removing old entries.


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:

@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.


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:

@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:

@CacheEvict (value="books", key = "#bookId")
public void deleteBook(Integer bookId) {...}

1.4. Configuring timeouts for cache operations

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.


  • Configure the following timeout properties in the context XML for your application on either SpringEmbeddedCacheManagerFactoryBean or SpringRemoteCacheManagerFactoryBean.

    For remote caches, you can also add these properties to the file.


Specifies the time, in milliseconds, to wait for read operations to complete. The default is 0 which means unlimited wait time.


Specifies the time, in milliseconds, to wait for write operations to complete. The default is 0 which means unlimited wait time.

The following example shows the timeout properties in the context XML for SpringRemoteCacheManagerFactoryBean:

<bean id="springRemoteCacheManagerConfiguredUsingConfigurationProperties"
    <property name="configurationProperties">
           <prop key="">500</prop>
           <prop key="infinispan.spring.operation.write.timeout">700</prop>