【问题标题】:How can I configure Ehcache 3 + spring boot + java config without xml?如何在没有 xml 的情况下配置 Ehcache 3 + spring boot + java config?
【发布时间】:2019-03-24 07:33:32
【问题描述】:

我使用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 bootJava config 而不是 xml 但我还没有找到任何示例。我的问题:

1) 为什么几乎所有的例子都是基于xml的?这怎么能比 java config 更好?

2) 如何不使用 xmlspring boot 中使用 java config 配置 Ehcache 3 >?

【问题讨论】:

  • 你看到this?
  • @Patrick 在本文中使用 xml 配置

标签: java spring spring-boot ehcache


【解决方案1】:

这是用于创建 Ehcache 管理器 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;

    }

}

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

【讨论】:

  • 如果您的缓存方法没有任何参数,您可以使用SimpleKey进行密钥类型配置。 CacheConfigurationBuilder&lt;SimpleKey, String&gt; configuration = ...
【解决方案2】:

有很多例子。我个人是 Java 配置的粉丝。

以下是主要的官方示例:

【讨论】:

    【解决方案3】:

    我更喜欢使用 JAVA 配置而不是 XML 配置。下面有两个代码示例,它们的逻辑相同,但风格不同。

    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;
        }
    
    }
    

    【讨论】:

    • 你的 XML 和 Java 方法似乎是一样的。
    • 我有两个带有 EhCacheManager 的模块,每个模块都以编程方式;一个与他的缓存,另一个与其他缓存,它们不共享,并且在 JMX 中注册。但是当我运行我的应用程序时,我得到了 InstanceAlreadyExistsException。如何在 JMX 中同时运行并显示仅有的两个实例?
    猜你喜欢
    • 2020-01-14
    • 2019-05-24
    • 2021-10-11
    • 2018-03-31
    • 2014-12-05
    • 2020-10-27
    • 2018-10-26
    • 2020-11-14
    • 2014-02-18
    相关资源
    最近更新 更多