spring集成redis(ehcache缓存改成redis)
生活随笔
收集整理的這篇文章主要介紹了
spring集成redis(ehcache缓存改成redis)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.先準備要需要的四個和redis相關的jar包
commons-pool2-2.4.2.jar、jedis-2.9.0.jar、spring-data-redis-1.8.16.RELEASE.jar、spring-data-commons-1.13.16.RELEASE.jar
把這個三個jar引進項目里
2.增加一個redis.propertes
#redis的服務器地址 redis.host=127.0.0.1 #redis的服務端口 redis.port=6379 #密碼 redis.password= #客戶端超時時間單位是毫秒 redis.timeout=100000 #最大建立連接等待時間 redis.maxWaitMillis=1000 #指明是否在從池中取出連接前進行檢驗,如果檢驗失敗,則從池中去除連接并嘗試取出另一個 redis.testOnBorrow=true ###################序列號客戶端 db0################################## #最大連接數 redis.maxTotal_db0=100 #最大空閑數 redis.maxIdle_db0=10 #最小空閑數 redis.minIdle_db0=2 ###################session db2###################################最大連接數 redis.maxTotal_db2=100 #最大空閑數 redis.maxIdle_db2=20 #最小空閑數 redis.minIdle_db2=53.把spring-cache.xml配置更改
先注釋以前的ehcache方式
然后改用redis
<?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:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:cache="http://www.springframework.org/schema/cache"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd"><!-- 啟用緩存注解功能 --><cache:annotation-driven cache-manager="cacheManager"proxy-target-class="true" /><!-- cacheManager工廠類 --><!-- <bean id="cacheManagerFactory"class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"p:configLocation="classpath:ehcache.xml" p:shared="true" /> --><!-- 聲明cacheManager --><!-- <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"p:cacheManager-ref="cacheManagerFactory" /> --><!-- spring自己的緩存管理器,這里定義了緩存位置名稱 ,即注解中的value --><bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager"><property name="caches"><set><!-- 登陸用戶訪問權限 --><bean class="org.springframework.data.redis.cache.RedisCache"><constructor-arg name="redisOperations" ref="redisTemplate_db2"/><constructor-arg name="name" value="accessDecisionCache"></constructor-arg><!--這是通用的前綴--><constructor-arg name="prefix" value="accessDecisionCache"></constructor-arg><!--這里的單位是秒--><constructor-arg name="expiration" value="7200"></constructor-arg></bean><!-- 動態表單 --><!-- <bean class="org.springframework.data.redis.cache.RedisCache"><constructor-arg name="redisOperations" ref="redisTemplate_db2"/><constructor-arg name="name" value="customFormCache"></constructor-arg>這是通用的前綴<constructor-arg name="prefix" value="customFormCache"></constructor-arg>這里的單位是秒<constructor-arg name="expiration" value="7200"></constructor-arg></bean> --></set></property></bean> </beans>特別注意:需要把ehcahe.xml里面的緩存信息改寫到spring-cache.xml,如上圖所示
4.加多一個redis包
里面添加一個config配置文件
package com.hecheng.redis.config;import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.repository.configuration.EnableRedisRepositories; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer;import redis.clients.jedis.JedisPoolConfig;import com.hecheng.core.model.EnumDictable; import com.hecheng.redis.config.RedisConfig;@Configuration @ComponentScan("com.hecheng.redis") @PropertySource("classpath:config/redis.properties") @EnableRedisRepositories("com.hecheng.redis") public class RedisConfig {@Value("${redis.host}")private String host;@Value("${redis.port}")private int port;@Value("${redis.password}")private String password;@Value("${redis.timeout}")private String timeout;@Value("${redis.maxWaitMillis}")private int maxWaitMillis;@Value("${redis.testOnBorrow}")private boolean testOnBorrow;/*** 序列號客戶端 db0*/@Value("${redis.maxTotal_db0}")private int maxTotal_db0;@Value("${redis.maxIdle_db0}")private int maxIdle_db0;@Value("${redis.minIdle_db0}")private int minIdle_db0;/*** ERPCACHE db2*/@Value("${redis.maxTotal_db2}")private int maxTotal_db2;@Value("${redis.maxIdle_db2}")private int maxIdle_db2;@Value("${redis.minIdle_db2}")private int minIdle_db2;public enum DatabaseEnum implements EnumDictable {序列號(0),ERPCACHE(2);private Integer value;DatabaseEnum(Integer value) {this.value = value;}public Integer getValue() {return this.value;}public String getLabel() {return this.name();}}/*********************************** 序列號客戶端 db0 **********************************************//*** 序列號客戶端 db0 默認* @return*/@BeanJedisConnectionFactory jedisConnectionFactory_db0() {// 創建連接池JedisPoolConfig poolConfig = new JedisPoolConfig();// 最大連接數poolConfig.setMaxTotal(maxTotal_db0);// 連接池中的最大空閑連接poolConfig.setMaxIdle(maxIdle_db0);// 連接池中的最小空閑連接poolConfig.setMinIdle(minIdle_db0);// 最大建立連接等待時間poolConfig.setMaxWaitMillis(maxWaitMillis);// #指明是否在從池中取出連接前進行檢驗,如果檢驗失敗,則從池中去除連接并嘗試取出另一個poolConfig.setTestOnBorrow(testOnBorrow);JedisConnectionFactory factory = new JedisConnectionFactory();factory.setHostName(host);factory.setPort(port);factory.setPassword(password);factory.setPoolConfig(poolConfig);factory.setDatabase(RedisConfig.DatabaseEnum.序列號.getValue());factory.setUsePool(true);return factory;}/*** 序列號客戶端 db0 默認客戶端* @return*/@BeanRedisTemplate<Object, Object> redisTemplate() {RedisTemplate<Object, Object> template = new RedisTemplate<Object,Object>();template.setConnectionFactory(jedisConnectionFactory_db0());template.setKeySerializer(new Jackson2JsonRedisSerializer<Object>(Object.class));template.setHashKeySerializer(new Jackson2JsonRedisSerializer<Object>(Object.class));template.setHashValueSerializer(new Jackson2JsonRedisSerializer<Object>(Object.class));template.setValueSerializer(new Jackson2JsonRedisSerializer<Object>(Object.class));return template;}/*********************************** ERPCACHE db2 **********************************************//*** ERPCACHE db2* @return*/@Bean(name="jedisConnectionFactory_db2")RedisConnectionFactory jedisConnectionFactory_db2() {JedisPoolConfig poolConfig = new JedisPoolConfig();// 最大連接數poolConfig.setMaxTotal(maxTotal_db2);// 連接池中的最大空閑連接poolConfig.setMaxIdle(maxIdle_db2);// 連接池中的最小空閑連接poolConfig.setMinIdle(minIdle_db2);// 最大建立連接等待時間poolConfig.setMaxWaitMillis(maxWaitMillis);// #指明是否在從池中取出連接前進行檢驗,如果檢驗失敗,則從池中去除連接并嘗試取出另一個poolConfig.setTestOnBorrow(testOnBorrow);JedisConnectionFactory factory = new JedisConnectionFactory();factory.setHostName(host);factory.setPort(port);factory.setPassword(password);factory.setPoolConfig(poolConfig);factory.setDatabase(RedisConfig.DatabaseEnum.ERPCACHE.getValue());factory.setUsePool(true);return factory;}/*** ERPCACHE 默認客戶端* @return*/@BeanRedisTemplate<String,Object> redisTemplate_db2() {RedisTemplate<String, Object> template = new RedisTemplate<String,Object>();template.setConnectionFactory(jedisConnectionFactory_db2());template.setKeySerializer(new StringRedisSerializer());return template;} }5.重啟一下項目,見證奇跡的時候來了
6.注意一下,redis本身是有16個庫的,所以才有最開始時候的db0,db1等不同庫的配置信息
最后來一個小提醒
?
總結
以上是生活随笔為你收集整理的spring集成redis(ehcache缓存改成redis)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 研究一个新的功能的时候,如何获取该资源的
- 下一篇: xml约束和实际场景使用