spring集成redis(ehcache缓存改成redis)

2024-06-08 12:48

本文主要是介绍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)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1042230

相关文章

Java中的Cursor使用详解

《Java中的Cursor使用详解》本文介绍了Java中的Cursor接口及其在大数据集处理中的优势,包括逐行读取、分页处理、流控制、动态改变查询、并发控制和减少网络流量等,感兴趣的朋友一起看看吧... 最近看代码,有一段代码涉及到Cursor,感觉写法挺有意思的。注意是Cursor,而不是Consumer

解决java.lang.NullPointerException问题(空指针异常)

《解决java.lang.NullPointerException问题(空指针异常)》本文详细介绍了Java中的NullPointerException异常及其常见原因,包括对象引用为null、数组元... 目录Java.lang.NullPointerException(空指针异常)NullPointer

javaScript在表单提交时获取表单数据的示例代码

《javaScript在表单提交时获取表单数据的示例代码》本文介绍了五种在JavaScript中获取表单数据的方法:使用FormData对象、手动提取表单数据、使用querySelector获取单个字... 方法 1:使用 FormData 对象FormData 是一个方便的内置对象,用于获取表单中的键值

前端知识点之Javascript选择输入框confirm用法

《前端知识点之Javascript选择输入框confirm用法》:本文主要介绍JavaScript中的confirm方法的基本用法、功能特点、注意事项及常见用途,文中通过代码介绍的非常详细,对大家... 目录1. 基本用法2. 功能特点①阻塞行为:confirm 对话框会阻塞脚本的执行,直到用户作出选择。②

Docker部署Jenkins持续集成(CI)工具的实现

《Docker部署Jenkins持续集成(CI)工具的实现》Jenkins是一个流行的开源自动化工具,广泛应用于持续集成(CI)和持续交付(CD)的环境中,本文介绍了使用Docker部署Jenkins... 目录前言一、准备工作二、设置变量和目录结构三、配置 docker 权限和网络四、启动 Jenkins

SpringBoot项目注入 traceId 追踪整个请求的日志链路(过程详解)

《SpringBoot项目注入traceId追踪整个请求的日志链路(过程详解)》本文介绍了如何在单体SpringBoot项目中通过手动实现过滤器或拦截器来注入traceId,以追踪整个请求的日志链... SpringBoot项目注入 traceId 来追踪整个请求的日志链路,有了 traceId, 我们在排

Java实战之利用POI生成Excel图表

《Java实战之利用POI生成Excel图表》ApachePOI是Java生态中处理Office文档的核心工具,这篇文章主要为大家详细介绍了如何在Excel中创建折线图,柱状图,饼图等常见图表,需要的... 目录一、环境配置与依赖管理二、数据源准备与工作表构建三、图表生成核心步骤1. 折线图(Line Ch

Spring Boot 3 整合 Spring Cloud Gateway实践过程

《SpringBoot3整合SpringCloudGateway实践过程》本文介绍了如何使用SpringCloudAlibaba2023.0.0.0版本构建一个微服务网关,包括统一路由、限... 目录引子为什么需要微服务网关实践1.统一路由2.限流防刷3.登录鉴权小结引子当前微服务架构已成为中大型系统的标

Java集合中的List超详细讲解

《Java集合中的List超详细讲解》本文详细介绍了Java集合框架中的List接口,包括其在集合中的位置、继承体系、常用操作和代码示例,以及不同实现类(如ArrayList、LinkedList和V... 目录一,List的继承体系二,List的常用操作及代码示例1,创建List实例2,增加元素3,访问元

Java中将异步调用转为同步的五种实现方法

《Java中将异步调用转为同步的五种实现方法》本文介绍了将异步调用转为同步阻塞模式的五种方法:wait/notify、ReentrantLock+Condition、Future、CountDownL... 目录异步与同步的核心区别方法一:使用wait/notify + synchronized代码示例关键