(转)使用 Spring缓存抽象 支持 EhCache 和 Redis 混合部署
生活随笔
收集整理的這篇文章主要介紹了
(转)使用 Spring缓存抽象 支持 EhCache 和 Redis 混合部署
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
背景:最近項目組在開發(fā)本地緩存,其中用到了redis和ehcache,但是在使用注解過程中發(fā)現(xiàn)兩者會出現(xiàn)沖突,這里給出解決兩者沖突的具體方案。
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提供統(tǒng)一的{@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();}/*** 構(gòu)建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);}}};}/*** 構(gòu)建默認(rèn)的鍵生成器* @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相關(guān)配置:
<?xml version="1.0" encoding="UTF-8"?><!-- Spring集成redis sentinel配置,對應(yīng)配置文件: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" /><!-- 支持事務(wù) --><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" /><!-- 默認(rèn)有效期,單位:秒 --><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
?
轉(zhuǎn)載于:https://www.cnblogs.com/lixuwu/p/8427029.html
總結(jié)
以上是生活随笔為你收集整理的(转)使用 Spring缓存抽象 支持 EhCache 和 Redis 混合部署的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 联通宽带多少钱一年啊?
- 下一篇: 光遇重塑药水怎么获得?