You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多
文档控制台
免费开始使用

如何实现EhCache 3 CacheManager与Spring及JCache CacheManager的适配?

当然可以搞定这个需求!下面给你两种实用的方案,对应你提到的两种思路:

方案一:直接将EhCache 3的CacheManager适配为Spring CacheManager

Spring框架本身就提供了对EhCache 3的原生支持,你只需要借助EhCacheCacheManager这个类就能完成转换。步骤如下:

  1. 引入必要依赖
    确保项目包含Spring缓存支持模块和EhCache 3的依赖,以Maven为例:

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>你的Spring版本</version>
    </dependency>
    <dependency>
        <groupId>org.ehcache</groupId>
        <artifactId>ehcache</artifactId>
        <version>你的EhCache 3版本</version>
    </dependency>
    
  2. 编写配置类
    创建Spring配置类,先初始化EhCache 3的CacheManager,再用EhCacheCacheManager把它包装成Spring标准的CacheManager

    import org.ehcache.CacheManager;
    import org.ehcache.config.builders.CacheManagerBuilder;
    import org.springframework.cache.annotation.EnableCaching;
    import org.springframework.cache.ehcache.EhCacheCacheManager;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    @EnableCaching
    public class CacheConfig {
    
        @Bean
        public CacheManager ehCache3CacheManager() {
            // 可根据需求自定义配置,比如添加缓存定义或加载xml配置文件
            return CacheManagerBuilder.newCacheManagerBuilder()
                    .withCache("myCache", 
                        org.ehcache.config.builders.CacheConfigurationBuilder.newCacheConfigurationBuilder(
                            String.class, Object.class, 
                            org.ehcache.config.builders.ResourcePoolsBuilder.heap(100)))
                    .build(true);
        }
    
        @Bean
        public org.springframework.cache.CacheManager springCacheManager(CacheManager ehCache3CacheManager) {
            return new EhCacheCacheManager(ehCache3CacheManager);
        }
    }
    

    配置完成后,Spring就能用这个适配后的CacheManager处理@Cacheable@CachePut等缓存注解了。

方案二:先转成JCache(javax.cache.CacheManager)再适配

既然你已经了解JCache到Spring CacheManager的适配方法,这个思路也很顺畅——EhCache 3完全实现了JSR-107(JCache)规范,转换起来毫无压力:

  1. 引入JCache依赖
    在之前的基础上,额外添加EhCache的JCache实现模块:

    <dependency>
        <groupId>org.ehcache</groupId>
        <artifactId>ehcache-jcache</artifactId>
        <version>你的EhCache 3版本</version>
    </dependency>
    
  2. 配置类实现转换
    通过EhCache的EhcacheCachingProvider获取JCache的CacheManager,再用Spring的JCacheCacheManager完成适配:

    import javax.cache.CacheManager;
    import org.ehcache.jsr107.EhcacheCachingProvider;
    import org.springframework.cache.annotation.EnableCaching;
    import org.springframework.cache.jcache.JCacheCacheManager;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    @EnableCaching
    public class JCacheConfig {
    
        @Bean
        public CacheManager jCacheManager() {
            EhcacheCachingProvider provider = new EhcacheCachingProvider();
            // 可加载EhCache 3的xml配置文件,也支持编程式配置
            return provider.getCacheManager(
                getClass().getResource("/ehcache.xml").toURI(),
                getClass().getClassLoader()
            );
        }
    
        @Bean
        public org.springframework.cache.CacheManager springCacheManager(CacheManager jCacheManager) {
            return new JCacheCacheManager(jCacheManager);
        }
    }
    

额外小贴士

  • 注意Spring和EhCache 3的版本兼容,比如Spring 5.2+搭配EhCache 3.8+是比较稳定的组合;
  • 如果用Spring Boot,配置可以更简化:只需指定spring.cache.type=ehcachespring.cache.type=jcache,框架会自动完成大部分配置工作。

内容的提问来源于stack exchange,提问作者Christina

火山引擎 最新活动