Настройка Ecache через java < /li>
/> ehcacheconfig.java
Код: Выделить всё
package com.owozniak.tradingplugin.configs;
import com.owozniak.tradingplugin.controllers.OhlcCandleResponse;
import com.owozniak.tradingplugin.controllers.OhlcCandleResult;
import org.ehcache.Cache;
import org.ehcache.PersistentCacheManager;
import org.ehcache.config.CacheConfiguration;
import org.ehcache.config.builders.*;
import org.ehcache.config.units.*;
import org.ehcache.jsr107.Eh107Configuration;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.cache.Caching;
import javax.cache.spi.CachingProvider;
import java.io.File;
import java.time.LocalDate;
import java.util.List;
import java.util.Set;
@Configuration
@EnableCaching
public class EhcacheConfig {
private static final File PERSISTENCE_DIR = new File("spring-boot-ehcache/cache");
@Bean
public PersistentCacheManager ehCacheNativeManager() {
return CacheManagerBuilder.newCacheManagerBuilder()
.with(CacheManagerBuilder.persistence(PERSISTENCE_DIR)) // Register persistence service
.withCache("lastTradingDay", getConfig(String.class, LocalDate.class))
.withCache("nextTradingDay", getConfig(String.class, LocalDate.class))
.withCache("getOhlc", getConfig(String.class, OhlcCandleResponse.class))
.withCache("getOhlcAll", getConfig(String.class, (Class) (Class) List.class))
.withCache("getTodaySymbols", getConfig(String.class, (Class) (Class) Set.class))
.build(true);
}
private static CacheConfiguration getConfig(Class keyType, Class valueType) {
return CacheConfigurationBuilder.newCacheConfigurationBuilder(
keyType,
valueType,
ResourcePoolsBuilder.newResourcePoolsBuilder()
.heap(10_000, EntryUnit.ENTRIES)
.offheap(1, MemoryUnit.GB)
.disk(10, MemoryUnit.GB, true)) // disk requires persistence config
.withExpiry(ExpiryPolicyBuilder.noExpiration())
.build();
}
@Bean
public javax.cache.CacheManager jCacheManager(PersistentCacheManager ehCacheNativeManager) {
CachingProvider provider = Caching.getCachingProvider();
javax.cache.CacheManager jCacheManager = provider.getCacheManager();
// Register each cache with the JSR-107 manager
ehCacheNativeManager.getRuntimeConfiguration().getCacheConfigurations().forEach((name, config) -> {
javax.cache.configuration.Configuration configuration = Eh107Configuration.fromEhcacheCacheConfiguration(config);
jCacheManager.createCache(name, configuration);
});
return jCacheManager;
}
@Bean
public org.springframework.cache.CacheManager cacheManager(javax.cache.CacheManager jCacheManager) {
return new org.springframework.cache.jcache.JCacheCacheManager(jCacheManager);
}
}
Подробнее здесь: https://stackoverflow.com/questions/796 ... ersistence