이 콘텐츠는 선택한 언어로 제공되지 않습니다.

Chapter 1. Using Embedded Caches


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

자세한 정보

평가판, 구매 및 판매

커뮤니티

Red Hat 문서 정보

Red Hat을 사용하는 고객은 신뢰할 수 있는 콘텐츠가 포함된 제품과 서비스를 통해 혁신하고 목표를 달성할 수 있습니다.

보다 포괄적 수용을 위한 오픈 소스 용어 교체

Red Hat은 코드, 문서, 웹 속성에서 문제가 있는 언어를 교체하기 위해 최선을 다하고 있습니다. 자세한 내용은 다음을 참조하세요.Red Hat 블로그.

Red Hat 소개

Red Hat은 기업이 핵심 데이터 센터에서 네트워크 에지에 이르기까지 플랫폼과 환경 전반에서 더 쉽게 작업할 수 있도록 강화된 솔루션을 제공합니다.

© 2024 Red Hat, Inc.