반응형
xml 없이 Ehcache 3 + 스프링 부트 + Java 구성을 구성하려면 어떻게 해야 합니까?
사용합니다Ehcache 2 + spring boot
다음은 내 구성입니다.
@Bean
public CacheManager cacheManager() {
return new EhCacheCacheManager(ehCacheCacheManager().getObject());
}
@Bean
public EhCacheManagerFactoryBean ehCacheCacheManager() {
EhCacheManagerFactoryBean cmfb = new EhCacheManagerFactoryBean();
cmfb.setConfigLocation(new ClassPathResource("ehcache.xml"));
cmfb.setShared(true);
return cmfb;
}
ehcache.xml - in resources.
이제 사용하고 싶습니다.Ehcache 3 + spring boot
xml 대신 Java 구성을 사용하지만 이에 대한 예를 찾을 수 없습니다.질문:
왜 거의 모든 예제가 xml을 기반으로 합니까? 어떻게 이것이 자바 구성보다 더 나을 수 있습니까?
xml을 사용하지 않고 봄 부팅에서 Java 구성을 사용하여 Ehcache 3을 구성하려면 어떻게 해야 합니까?
다음은 Ehcache Manager bean을 생성하기 위한 동등한 Java 구성입니다.
ApplicationConfig.java
@Configuration
@EnableCaching
public class ApplicationConfig {
@Bean
public CacheManager ehCacheManager() {
CachingProvider provider = Caching.getCachingProvider();
CacheManager cacheManager = provider.getCacheManager();
CacheConfigurationBuilder<String, String> configuration =
CacheConfigurationBuilder.newCacheConfigurationBuilder(
String.class,
String.class,
ResourcePoolsBuilder
.newResourcePoolsBuilder().offheap(1, MemoryUnit.MB))
.withExpiry(ExpiryPolicyBuilder.timeToLiveExpiration(Duration.ofSeconds(20)));
javax.cache.configuration.Configuration<String, String> stringDoubleConfiguration =
Eh107Configuration.fromEhcacheCacheConfiguration(configuration);
cacheManager.createCache("users", stringDoubleConfiguration);
return cacheManager;
}
}
서비스 Impl.java
@Service
public class ServiceImpl {
@Cacheable(cacheNames = {"users"})
public String getThis(String id) {
System.out.println("Method called............");
return "Value "+ id;
}
}
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>javax.cache</groupId>
<artifactId>cache-api</artifactId>
</dependency>
<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
</dependency>
많은 예들이 있습니다.저는 개인적으로 Java 구성의 팬입니다.
다음은 주요 공식적인 예입니다.
- 기본 프로그램 구성
- 전체 날개 스프링 부트 구성(클러스터링 포함)
- Spring Pet Clinic을 통한 간단한 구성
XML 구성보다 JAVA 구성을 사용하는 것을 선호합니다.아래에는 동일한 논리를 수행하지만 다른 스타일로 작성되는 코드의 두 가지 예가 있습니다.
XML 접근 방식:
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.cache.ehcache.EhCacheManagerFactoryBean;
import org.springframework.cache.support.CompositeCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.ClassPathResource;
import java.util.Collections;
@EnableCaching
@Configuration
public class CacheConfig {
@Primary
@Bean("cacheManager")
public CompositeCacheManager cacheManager() {
CompositeCacheManager compositeCacheManager = new CompositeCacheManager();
compositeCacheManager.setFallbackToNoOpCache(true);
compositeCacheManager.setCacheManagers(Collections.singleton(ehCacheManager()));
return compositeCacheManager;
}
@Bean("ehCacheManager")
public EhCacheCacheManager ehCacheManager() {
EhCacheCacheManager ehCacheCacheManager = new EhCacheCacheManager();
ehCacheCacheManager.setCacheManager(ehCacheManagerFactoryBean().getObject());
return ehCacheCacheManager;
}
@Bean
public EhCacheManagerFactoryBean ehCacheManagerFactoryBean() {
EhCacheManagerFactoryBean ehCacheManagerFactoryBean = new EhCacheManagerFactoryBean();
ehCacheManagerFactoryBean.setShared(true);
ehCacheManagerFactoryBean.setConfigLocation(new ClassPathResource("ehcache.xml"));
return ehCacheManagerFactoryBean;
}
}
ehcache.xml
<ehcache name="custom_eh_cache">
<diskStore path="java.io.tmpdir"/>
<cache name="users"
maxElementsInMemory="1000"
eternal="false"
timeToIdleSeconds="3600"
timeToLiveSeconds="3600"
memoryStoreEvictionPolicy="LRU"/>
<cache name="roles"
maxElementsInMemory="1000"
eternal="false"
timeToIdleSeconds="3600"
timeToLiveSeconds="3600"
memoryStoreEvictionPolicy="LRU"/>
</ehcache>
JAVA 접근 방식:
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.config.CacheConfiguration;
import net.sf.ehcache.config.DiskStoreConfiguration;
import net.sf.ehcache.store.MemoryStoreEvictionPolicy;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.cache.support.CompositeCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import java.util.Collections;
@EnableCaching
@Configuration
public class CacheConfig {
@Primary
@Bean("cacheManager")
public CompositeCacheManager cacheManager() {
CompositeCacheManager compositeCacheManager = new CompositeCacheManager();
compositeCacheManager.setFallbackToNoOpCache(true);
compositeCacheManager.setCacheManagers(Collections.singleton(ehCacheManager()));
return compositeCacheManager;
}
@Bean("ehCacheManager")
public EhCacheCacheManager ehCacheManager() {
EhCacheCacheManager ehCacheCacheManager = new EhCacheCacheManager();
ehCacheCacheManager.setCacheManager(getCustomCacheManager());
return ehCacheCacheManager;
}
private CacheManager getCustomCacheManager() {
CacheManager cacheManager = CacheManager.create(getEhCacheConfiguration());
cacheManager.setName("custom_eh_cache");
cacheManager.addCache(createCache("users"));
cacheManager.addCache(createCache("roles"));
return cacheManager;
}
private Cache createCache(String cacheName) {
CacheConfiguration cacheConfig = new CacheConfiguration(cacheName, 1000)
.memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LRU)
.eternal(false)
.timeToLiveSeconds(3600)
.timeToIdleSeconds(3600);
return new Cache(cacheConfig);
}
private net.sf.ehcache.config.Configuration getEhCacheConfiguration() {
net.sf.ehcache.config.Configuration configuration = new net.sf.ehcache.config.Configuration();
DiskStoreConfiguration diskStoreConfiguration = new DiskStoreConfiguration();
diskStoreConfiguration.setPath("java.io.tmpdir");
configuration.addDiskStore(diskStoreConfiguration);
return configuration;
}
}
언급URL : https://stackoverflow.com/questions/52891844/how-can-i-configure-ehcache-3-spring-boot-java-config-without-xml
반응형
'source' 카테고리의 다른 글
음의 시간 범위 형식 지정 (0) | 2023.07.16 |
---|---|
Django Rest Framework - 보기 이름 "user-detail"을 사용하여 하이퍼링크된 관계의 URL을 확인할 수 없습니다. (0) | 2023.07.16 |
angular2 테스트를 위해 http 오류를 모의하는 방법 (0) | 2023.07.16 |
Oracle: 쿼리를 통해 전체 비율을 얻는 방법은 무엇입니까? (0) | 2023.07.16 |
Python Logging을 사용하여 두 번 나타나는 로그 메시지 (0) | 2023.07.16 |