spring cache CacheException: Another unnamed CacheManager already exists in the same VM. Please

在尝试将Spring Cache与Redis及Ehcache集成时遇到问题。使用spring-boot-starter-data-redis包时,集成Redis作为缓存,而Hibernate的二级缓存使用Ehcache。尝试同时用Redis和Ehcache做系统缓存导致冲突,Redis配置会覆盖Ehcache。当尝试在Java配置中同时配置两者时,由于net.sf.ehcache.CacheManager的单例特性,会报错。最终通过特定方法创建新的CacheManager解决了问题。
摘要由CSDN通过智能技术生成

springboot1.x系列中,spring-boot-starter-data-redis包客户端使用的是jedis,但是到了springboot2.x其中使用的是Lettuce。 

用redis做缓存时,有以下两种方式:

①与spring-cache集成,查询时缓存,增删改时删除缓存

②写个redis工具类,自己在需要的地方写插入和查询的方法

 

可以同时使用ehcache做系统缓存和hibernate的二级缓存

spring:
  jpa:
    properties:
      hibernate:
        cache:
          # 二级缓存 针对实体的
          use_second_level_cache: true
          provider_configuration_file_resource_path: ehcache-hibernate.xml
          region:
            factory_class: org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory
  cache:
    type: ehcache
    ehcache:
      config: classpath:ehcache-spring.xml

问题是我想同时用redis和ehcache 和spring-cache集成做系统缓存,同时又用ehcache做hibernate的二级缓存,

这就尴尬了。

①yml中配置系统ehcache,java config 配置redisCache,redis会覆盖掉ehcache,如下源码。


                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值