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=5
3.把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)相关推荐
- Spring集成Spring-data-redis RedisCacheManager缓存源码分析
在项目中,一般我们会把服务层(service)的一些查询结果和一些数据进行缓存.缓存的种类有很多.这里进行redis作为缓存框架,进行一个缓存的配置.配置前需要先了解一些基本的知识 在Spring中缓 ...
- php使用redis做缓存,php 使用 redis 的缓存实例
本篇文章给大家分享的内容是php 使用 redis 的缓存实例,有着一定的参考价值,有需要的朋友可以参考一下 最近刚开始研究redis,就写了一个php 使用 redis 的缓存小实例,不喜勿喷 大致 ...
- php使用redis做缓存,php使用redis做缓存和使用redis保存session
php使用redis 一.ubuntu上安装php的redis模块 直接使用命令:sudo apt install php-redis 重启php-fpm: /etc/init.d/php7.3-fp ...
- springboot mybatis 项目框架源码 shiro 集成代码生成器 ehcache缓存
1.代码生成器: [正反双向](单表.主表.明细表.树形表,快速开发利器) freemaker模版技术 ,0个代码不用写,生成完整的一个模块,带页面.建表sql脚本.处理类.service等完整模块 ...
- Window平台搭建Redis分布式缓存集群 ---redis 安装和使用
百度定义:Redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sorted ...
- redis分布式缓存php,基于redis分布式缓存实现
第一:Redis 是什么? Redis是基于内存.可持久化的日志型.Key-Value数据库 高性能存储系统,并提供多种语言的API. 第二:出现背景数据结构(Data Structure)需求越来越 ...
- redis清理缓存_大话Redis问题
使用Redis过程中,总是会遇到各种各样问题,这里进行问题的总结,作为Redis 进阶的经验分享. 更新缓存的问题 [主动]需要操作人员去操作,或者定时调度 [被动]由用户触发更新 [预加载]提前加载 ...
- Spring MVC学习总结(7)——Spring MVC整合Ehcache缓存框架
Ehcache算是当前比较流行的缓存框架,使用缓存可以极大的缓解服务器和数据库的压力,提高访问效率,提高服务器的并发能力.接下来我们看怎么把缓存使用起来. SpringMVC集成Ehcache所需的j ...
- Spring MVC整合Ehcache缓存框架
https://blog.csdn.net/u012562943/article/details/52289433 转载于:https://www.cnblogs.com/pjlhf/p/881874 ...
最新文章
- SQLite入门与分析(二)---设计与概念
- java程序a-z_用Java写一个程序:使其输出从A-Z的排序字母
- EasyUI datagrid控件的基本使用
- 栈与队列基本操作及其应用
- Keras-常用代码
- 使用wireshark抓包分析实战
- excel数据库_EXCEL数据库函数dcount、dcounta
- php程序员 合川_重庆PHP程序员工资如何?
- 燃烧的远征服务器排队小程序,请排队-在线排队叫号微信小程序
- bitcoin rpc command
- 影响手机成像质量的因素
- 创新工场汪华:给早期创业者的44条建议
- 如何将视频轻松转换为 GIF
- 售价对标奢侈品,国货香水“德不配位”?
- 操作系统相关基础知识
- Windows程式开发设计指南(十五)与装置无关的点阵图
- c语言栈的实现以及操作
- 教你如何用好MindMapper
- ssas连接oracle性能,Analysis Services(SSAS) 性能优化
- 阿里云服务器入门基础教程汇总(新手)