Questo contenuto non è disponibile nella lingua selezionata.

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. Upgrading Spring integration in Data Grid

1.1.1. From Spring 6 to Spring 7

Data Grid supports both Spring 6 and Spring 7. To migrate, simply update your Maven dependencies.

  • infinispan-spring6-embedded by infinispan-spring7-embedded
  • infinispan-spring6-remote by infinispan-spring7-remote

Backward compatibility is maintained, and no functionality should be affected.

1.1.2. From Spring Boot 3.x to Spring Boot 4.x

Data Grid supports both Spring Boot 3.x and Spring Boot 4.x. To migrate, first update your Maven dependencies. * infinispan-spring-boot3-starter-embedded by infinispan-spring-boot4-starter-embedded * infinispan-spring-boot3-starter-remote by infinispan-spring-boot4-starter-remote

In Embedded mode, the InfinispanConfigurationCustomizer bean has been deprecated in Spring Boot 3.x and removed in Spring Boot 4 integration. This component was used to customize the default cache, which was automatically created. Going forward, use InfinispanCacheConfigurer and explicitly define a cache named default if needed.

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

Important

Data Grid supports Spring version version 6 and version 7.

Procedure

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

    • Spring 6 Remote caches: infinispan-spring6-remote
    • Spring 7 Remote caches: infinispan-spring7-remote
    • Spring 6 Embedded caches: infinispan-spring6-embedded
    • Spring 7 Embedded caches: infinispan-spring7-embedded

      Tip

      Spring Boot users can add the following artifacts instead of the infinispan-spring6-embedded or infinispan-spring7-embedded:

      • For Spring Boot 3.x add infinispan-spring-boot3-starter-embedded
      • For Spring Boot 4.x add infinispan-spring-boot4-starter-embedded
  2. Configure your Hot Rod client to connect to your Data Grid Server deployment in the hotrod-client.properties file.

    infinispan.client.hotrod.server_list = 127.0.0.1:11222
    infinispan.client.hotrod.auth_username=admin
    infinispan.client.hotrod.auth_password=changeme

Spring Cache dependencies

Remote caches Spring 6

<dependencies>
    <dependency>
        <groupId>org.infinispan</groupId>
        <artifactId>infinispan-spring6-remote</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${version.spring}</version>
    </dependency>
</dependencies>

Remote caches Spring 7

<dependencies>
    <dependency>
        <groupId>org.infinispan</groupId>
        <artifactId>infinispan-spring7-remote</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${version.spring}</version>
    </dependency>
</dependencies>

Embedded caches Spring 6

<dependencies>
    <dependency>
        <groupId>org.infinispan</groupId>
        <artifactId>infinispan-spring6-embedded</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${version.spring}</version>
    </dependency>
</dependencies>

Embedded caches Spring 7

<dependencies>
    <dependency>
        <groupId>org.infinispan</groupId>
        <artifactId>infinispan-spring7-embedded</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${version.spring}</version>
    </dependency>
</dependencies>

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

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

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

    Declarative

    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:cache="http://www.springframework.org/schema/cache"
        xmlns:p="http://www.springframework.org/schema/p"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
    
            <cache:annotation-driven />
    
    </beans>

    Programmatic

    @EnableCaching @Configuration
    public class Config {
    }

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

    Tip

    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.4. Spring Cache annotations

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) {...}

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() {...}

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) {...}

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

Procedure

  • 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 hotrod-client.properties file.

Expand
PropertyDescription

infinispan.spring.operation.read.timeout

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

infinispan.spring.operation.write.timeout

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"
      class="org.infinispan.spring.remote.provider.SpringRemoteCacheManagerFactoryBean">
    <property name="configurationProperties">
        <props>
           <prop key="infinispan.spring.operation.read.timeout">500</prop>
           <prop key="infinispan.spring.operation.write.timeout">700</prop>
        </props>
    </property>
</bean>
Red Hat logoGithubredditYoutubeTwitter

Formazione

Prova, acquista e vendi

Community

Informazioni sulla documentazione di Red Hat

Aiutiamo gli utenti Red Hat a innovarsi e raggiungere i propri obiettivi con i nostri prodotti e servizi grazie a contenuti di cui possono fidarsi. Esplora i nostri ultimi aggiornamenti.

Rendiamo l’open source più inclusivo

Red Hat si impegna a sostituire il linguaggio problematico nel codice, nella documentazione e nelle proprietà web. Per maggiori dettagli, visita il Blog di Red Hat.

Informazioni su Red Hat

Forniamo soluzioni consolidate che rendono più semplice per le aziende lavorare su piattaforme e ambienti diversi, dal datacenter centrale all'edge della rete.

Theme

© 2026 Red Hat
Torna in cima