背景:最近项目组在开发本地缓存,其中用到了redis和ehcache,但是在使用注解过程中发现两者会出现冲突,这里给出解决两者冲突的具体方案。

spring-ehcache.xml配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cache="http://www.springframework.org/schema/cache"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/cachehttp://www.springframework.org/schema/cache/spring-cache-4.0.xsd"><description>ehcache缓存配置管理文件</description><bean id="ehCacheManagerFactory"class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"><property name="configLocation" value="classpath:ehcache/ehcache.xml" /></bean><bean id="ehCacheCacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"><property name="cacheManager" ref="ehCacheManagerFactory" /></bean></beans>

整合Ehcache和Redis的cacheManager,并注入容器:

package org.szfs.basic.middle.cache;import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;/*** 缓存集成配置类<p>* * 该类通过向spring提供统一的{@link CacheManager}以达到集成不同缓存中间件的目的,v0.1包括redis与ehcache<br>* 该类重写了{@link CachingConfigurerSupport#cacheManager()}和{@link CachingConfigurerSupport#keyGenerator()}的方法* * @author 51* @version $Id: SzfsCacheConfig.java, v 0.1 2018年2月5日 下午6:45:44 51 Exp $* * @see org.springframework.cache.annotation.CachingConfigurerSupport*/
@Configuration
@EnableCaching
public class SzfsCacheConfig extends CachingConfigurerSupport {/*** redis缓存前缀<p>* 用于通过缓存名前缀识别缓存中间件*/private static final String          REDIS_PREFIX = "redis-";@Autowired(required = false)private volatile RedisCacheManager   redisCacheManager;@Autowired(required = false)private volatile EhCacheCacheManager ehCacheCacheManager;public SzfsCacheConfig() {super();}/*** 构建CacheManager的实例szfsCacheManager<p>* * szfsCacheManager通过缓存名称<b>前缀</b>来识别缓存类型:* <ul>*   <li>"redis-": redis cache</li>*   <li> others : ehcache cache</li>* </ul>* 该实例是spring操作缓存所必须的,且需要保证唯一,这意味着,如果还需要Spring集成其它缓存中间件,仍然需要集成到szfsCacheManager<br>* * @see org.springframework.cache.annotation.CachingConfigurerSupport#cacheManager()*/@Bean("szfsCacheManager")@Overridepublic CacheManager cacheManager() {return new CacheManager() {@Overridepublic Collection<String> getCacheNames() {Collection<String> cacheNames = new ArrayList<String>();if (redisCacheManager != null) {cacheNames.addAll(redisCacheManager.getCacheNames());}if (ehCacheCacheManager != null) {cacheNames.addAll(ehCacheCacheManager.getCacheNames());}return cacheNames;}@Overridepublic Cache getCache(String name) {if (name.startsWith(REDIS_PREFIX)) {return redisCacheManager == null ? null : redisCacheManager.getCache(name);} else {return ehCacheCacheManager == null ? null : ehCacheCacheManager.getCache(name);}}};}/*** 构建默认的键生成器* @see org.springframework.cache.annotation.CachingConfigurerSupport#keyGenerator()*/@Bean@Overridepublic KeyGenerator keyGenerator() {return new KeyGenerator() {@Overridepublic Object generate(Object target, Method method, Object... params) {StringBuilder keyStrBuilder = new StringBuilder();keyStrBuilder.append(target.getClass().getName());keyStrBuilder.append(method.getName());for (Object _param : params) {keyStrBuilder.append(_param.toString());}return keyStrBuilder.toString();}};}public RedisCacheManager getRedisCacheManager() {return redisCacheManager;}public void setRedisCacheManager(RedisCacheManager redisCacheManager) {this.redisCacheManager = redisCacheManager;}public EhCacheCacheManager getEhCacheCacheManager() {return ehCacheCacheManager;}public void setEhCacheCacheManager(EhCacheCacheManager ehCacheCacheManager) {this.ehCacheCacheManager = ehCacheCacheManager;}}

redis相关配置:

<?xml version="1.0" encoding="UTF-8"?><!-- Spring集成redis sentinel配置,对应配置文件:redis-sentinel.properties --><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cache="http://www.springframework.org/schema/cache"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd"><description>Spring集成redis岗哨配置</description><!--配置 jedis pool --><bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"><property name="maxTotal" value="${redis.sentinel.jedis.pool.maxTotal}" /><property name="maxIdle" value="${redis.sentinel.jedis.pool.maxIdle}" /><property name="minIdle" value="${redis.sentinel.jedis.pool.minIdle}" /><property name="numTestsPerEvictionRun"value="${redis.sentinel.jedis.pool.numTestsPerEvictionRun}" /><property name="timeBetweenEvictionRunsMillis"value="${redis.sentinel.jedis.pool.timeBetweenEvictionRunsMillis}" /><property name="minEvictableIdleTimeMillis"value="${redis.sentinel.jedis.pool.minEvictableIdleTimeMillis}" /><property name="softMinEvictableIdleTimeMillis"value="${redis.sentinel.jedis.pool.softMinEvictableIdleTimeMillis}" /><property name="maxWaitMillis" value="${redis.sentinel.jedis.pool.maxWaitMillis}" /><property name="testOnBorrow" value="${redis.sentinel.jedis.pool.testOnBorrow}" /><property name="testWhileIdle" value="${redis.sentinel.jedis.pool.testWhileIdle}" /><property name="blockWhenExhausted"value="${redis.sentinel.jedis.pool.blockWhenExhausted}" /></bean><!-- 配置redisSentinelConfiguration --><bean id="redisSentinelConfiguration"class="org.springframework.data.redis.connection.RedisSentinelConfiguration"><property name="master"><bean class="org.springframework.data.redis.connection.RedisNode"><property name="name" value="mymaster" /></bean></property><property name="sentinels"><set><bean class="org.springframework.data.redis.connection.RedisNode"><constructor-arg name="host"value="${redis.sentinel.node1.host}" /><constructor-arg name="port"value="${redis.sentinel.node1.port}" /></bean><bean class="org.springframework.data.redis.connection.RedisNode"><constructor-arg name="host"value="${redis.sentinel.node2.host}" /><constructor-arg name="port"value="${redis.sentinel.node2.port}" /></bean><bean class="org.springframework.data.redis.connection.RedisNode"><constructor-arg name="host"value="${redis.sentinel.node3.host}" /><constructor-arg name="port"value="${redis.sentinel.node3.port}" /></bean></set></property></bean><!-- 配置JedisConnectionFactory --><bean id="jedisConnectionFactory"class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"><property name="password" value="${redis.sentinel.password}" /><constructor-arg ref="jedisPoolConfig" /><constructor-arg ref="redisSentinelConfiguration" /></bean><!-- 配置redisTemplate --><bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"><property name="connectionFactory" ref="jedisConnectionFactory" /><!-- 支持事务 --><property name="enableTransactionSupport" value="true" /><property name="keySerializer"><beanclass="org.springframework.data.redis.serializer.StringRedisSerializer" /></property><property name="valueSerializer"><beanclass="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer" /></property></bean><!-- 配置redisCacheManager --><bean id="redisCacheManager" class="org.springframework.data.redis.cache.RedisCacheManager"><constructor-arg name="redisOperations" ref="redisTemplate" /><!-- 默认有效期,单位:秒 --><property name="defaultExpiration" value="3600" /><!-- 多个缓存有效期,单位:秒 --><property name="expires"><map><entry key="redis-users" value="10" /></map></property></bean><!-- 配置RedisLockRegistry --><bean id="redisLockRegistry"class="org.springframework.integration.redis.util.RedisLockRegistry"><constructor-argtype="org.springframework.data.redis.connection.RedisConnectionFactory"ref="jedisConnectionFactory" /><constructor-arg type="java.lang.String" value="${redis.sentinel.registry.key}" /></bean></beans>

参考链接:http://blog.csdn.net/pmlpml/article/details/53116377

转载于:https://www.cnblogs.com/lixuwu/p/8427029.html

(转)使用 Spring缓存抽象 支持 EhCache 和 Redis 混合部署相关推荐

  1. 简单的Spring Memcached – Spring缓存抽象和Memcached

    在任何读取繁重的数据库应用程序中,缓存仍然是最基本的性能增强机制之一. Spring 3.1发行版提供了一个很酷的新功能,称为Cache Abstraction . Spring Cache Abst ...

  2. 三大缓存框架(Ehcache+Memcache+Redis)基础

    1.Ehcache(纯Java的进程内缓存框架,也叫二级缓存) Ehcache是一个开源的.设计于提高在数据从RDBMS中取出来的高花费.高延迟采取的一种缓存方案(在Java项目广泛的 使用).正因为 ...

  3. Spring测试上下文缓存+ AspectJ @Transactional + Ehcache的痛苦

    您在使用AspectJ @Transactionals和Spring吗? 您是否有多个SessionFactory,也许一个用于嵌入式数据库进行单元测试,一个用于实际数据库进行集成测试? 您是否遇到这 ...

  4. Spring指南之使用Spring缓存数据(Spring Framework官方文档之缓存抽象详解)

    1.请参见官方文档Spring指南之使用 Spring 缓存数据 2.请参见Spring官方文档之缓存抽象 3.参见github代码 文章目录 一.简介 二.你将创造什么(What You Will ...

  5. 缓存插件 Spring支持EHCache缓存

    Spring仅仅是提供了对缓存的支持,但它并没有任何的缓存功能的实现,spring使用的是第三方的缓存框架来实现缓存的功能.其中,spring对EHCache提供了很好的支持. 在介绍Spring的缓 ...

  6. Spring Cache抽象-缓存注解

    文章目录 概述 Spring缓存的基本原理 @Cacheable :主要针对方法配置,能够根据方法的请求参数对其结果进行缓存 键生成器 带条件的缓存 @Cacheable 注解参数说明 示例-缓存管理 ...

  7. Spring 3.1缓存抽象教程

    即将发布的Spring 3.1版本中引入的新功能之一是缓存抽象之一 . Spring Framework提供了对将缓存透明添加到现有Spring应用程序中的支持. 与事务支持类似,缓存抽象允许一致使用 ...

  8. Java缓存学习之五:spring 对缓存的支持

    (注意标题,Spring对缓存的支持 这里不单单指Ehcache ) 从3.1开始,Spring引入了对Cache的支持.其使用方法和原理都类似于Spring对事务管理的支持.Spring Cache ...

  9. Spring Cache抽象-基于XML的配置声明(基于EhCache的配置)

    概述 完整示例 pomxml增加依赖 数据库表数据Oracle 实体类 服务层 ehcache的配置文件 Spring-EhCache配置文件 单元测试 日志输出 日志分析 示例源码 概述 首先请阅读 ...

最新文章

  1. [Vim 使用]vim 自动括号补全配置
  2. 有了“手掌”,机械手也能盘“核桃”,耶鲁出品 | Science子刊
  3. ASP.NET页面的生命周期
  4. MySQL- SHOW TABLE STATUS命令
  5. 《几何与代数导引》习题1.38
  6. Spring-AOP @AspectJ语法基础
  7. 部署GitLab时, 问题
  8. Java---类加载
  9. 银河麒麟通过命令行安装软件没有安装上
  10. Javaweb安全——Java类加载机制
  11. 生鲜行业采购管理系统把控采购成本,实现精细化管理
  12. ubuntu18.04 pybluez pip3安装
  13. 《精进:如何成为一个很厉害的人》 采铜
  14. 随记 C#读取TXT文件乱码
  15. java SSM 班级同学录聚会报名网站-毕业设计源码介绍
  16. 山东畜牧兽医职业学院计算机考试,山东畜牧兽医职业学院计算机自编word15套试题11Word模拟试题(1-15).doc...
  17. H3C光模块相关命令和检测方法
  18. IDEA 学生授权申请方式(免费)
  19. ucos-ii操作系统
  20. uni-app 上架应用商店踩坑过程

热门文章

  1. python我的所得税计算器_教你使用Python实现新个税计算器
  2. rn php,rn怎样在PHP的正则表达式中匹配到?
  3. 用spss做多组两两相关性分析_两独立样本T检验及如何利用SPSS实现其操作
  4. 多线程处理缓慢_华为昇腾,AI推理性能超越对手一倍:软件挖掘处理器全部潜力...
  5. c语言交错级数前10项和,怎么求一个交错级数的和,谢谢
  6. 无法使用_解决kali linux 2020 安装完后发现无法使用 ifconfig
  7. php和架构,结构和架构的区别是什么?
  8. Linux多线程同步------条件变量
  9. pr 文件结构不一致_建筑工程合同与招投标文件不一致,工程结款应该以哪份文件为主?...
  10. 国产芯片WiFi物联网智能插座—电耗采集功能设计