Spring集成Redis过程中遇到的问题

本文介绍了在Spring项目中集成Redis以优化缓存查询时遇到的问题,详细阐述了集成过程,包括配置redis.properties、引入配置文件、创建spring-redis.xml等步骤。在集成过程中遇到maxWaitMillis属性错误,通过排查发现是由于不同版本的jedis和spring-data-redis之间的不兼容问题。最终通过调整版本和清理旧jar包成功解决问题。
摘要由CSDN通过智能技术生成

最近在开发项目的时候遇到如下的情况:

  • 前台list的属性由于数据量较大,而且在后台是递归获取的,所以前端展示的页面就比较慢,所以想集成一下redis,加快数据的查询。

Spring集成redis的主流方式大致有以下两种(以单机为例)

(1)依赖于spring-data-redis和jedis(基于java的redis)

  • 我的配置过程如下:
    因为是二次开发,所以是基于原有的SSM框架(Spring+Spring MVC+Mybatis)
    已有的配置我就不再次赘述了

1)首先新建数据库配置文件redis.properties,类似有Mysql的配置

redis.host = localhost
redis.port = 6379
redis.password = redis
redis.maxIdle=300
redis.maxWait=2000
redis.testOnBorrow = true
redis.timeout=5000

2)在spring-jdbc.xml中引入上述配置文件

	<!-- 引入配置文件 -->
	<context:property-placeholder location="classpath:db.properties,classpath:redis.properties"/>

这里要明确说明一下,因为spring容器在加载的时候只会加载一个实例,所以如果想加载两个以上,有以下几种方法:

  • 可以像上述格式一样,每一个配置文件都写一个classpath,用“,”隔开
  • 也可以用“*.properties”
  • 如果配置文件分开写,即jdbc和redis中都加载各自的,可以使用ignoreUnresolvablePlaceholders进行设置,两个xml都要写!

3)新建spring-redis.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:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值