Suchen

Dieser Inhalt ist in der von Ihnen ausgewählten Sprache nicht verfügbar.

Chapter 1. Using Embedded Caches

download PDF

Embed Data Grid caches directly in your project for in-memory data storage.

1.1. Adding the EmbeddedCacheManager Bean

Configure your application to use embedded caches.

Procedure

  1. Add infinispan-spring-boot3-starter-embedded to your project’s classpath to enable Embedded mode.
  2. Use the Spring @Autowired annotation to include an EmbeddedCacheManager bean in your Java configuration classes, as in the following example:

    private final EmbeddedCacheManager cacheManager;
    
    @Autowired
    public YourClassName(EmbeddedCacheManager cacheManager) {
        this.cacheManager = cacheManager;
    }

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"));

1.2. Using the reactive mode with Reactor

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

1.3. Cache Manager Configuration Beans

You can customize the Cache Manager with the following configuration beans:

  • InfinispanGlobalConfigurer
  • InfinispanCacheConfigurer
  • Configuration
  • InfinispanConfigurationCustomizer
  • InfinispanGlobalConfigurationCustomizer
Note

You can create one InfinispanGlobalConfigurer bean only. However you can create multiple configurations with the other beans.

InfinispanCacheConfigurer Bean

@Bean
public InfinispanCacheConfigurer cacheConfigurer() {
	return manager -> {
		final Configuration ispnConfig = new ConfigurationBuilder()
                        .clustering()
                        .cacheMode(CacheMode.LOCAL)
                        .build();

		manager.defineConfiguration("local-sync-config", ispnConfig);
	};
}

Configuration Bean

Link the bean name to the cache that it configures, as follows:

@Bean(name = "small-cache")
public org.infinispan.configuration.cache.Configuration smallCache() {
    return new ConfigurationBuilder()
        .read(baseCache)
        .memory().size(1000L)
        .memory().evictionType(EvictionType.COUNT)
        .build();
}

@Bean(name = "large-cache")
public org.infinispan.configuration.cache.Configuration largeCache() {
    return new ConfigurationBuilder()
        .read(baseCache)
        .memory().size(2000L)
        .build();
}

Customizer Beans

@Bean
public InfinispanGlobalConfigurationCustomizer globalCustomizer() {
   return builder -> builder.transport().clusterName(CLUSTER_NAME);
}

@Bean
public InfinispanConfigurationCustomizer configurationCustomizer() {
   return builder -> builder.memory().evictionType(EvictionType.COUNT);
}

1.4. 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 new SpringEmbeddedCacheManager.
  • RemoteCacheManager bean, it instantiates a new SpringRemoteCacheManager.
Red Hat logoGithubRedditYoutubeTwitter

Lernen

Testen, kaufen und verkaufen

Communitys

Über Red Hat Dokumentation

Wir helfen Red Hat Benutzern, mit unseren Produkten und Diensten innovativ zu sein und ihre Ziele zu erreichen – mit Inhalten, denen sie vertrauen können.

Mehr Inklusion in Open Source

Red Hat hat sich verpflichtet, problematische Sprache in unserem Code, unserer Dokumentation und unseren Web-Eigenschaften zu ersetzen. Weitere Einzelheiten finden Sie in Red Hat Blog.

Über Red Hat

Wir liefern gehärtete Lösungen, die es Unternehmen leichter machen, plattform- und umgebungsübergreifend zu arbeiten, vom zentralen Rechenzentrum bis zum Netzwerkrand.

© 2024 Red Hat, Inc.