Spring 4 集成 redis 实现缓存 一

本文介绍了在面临高并发和海量数据查询时,如何利用Spring 4集成Redis来构建缓存系统,以减轻数据库压力。内容包括集成的背景介绍、依赖引入、配置连接池,以及如何在单机版和集群版中操作Redis进行缓存管理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

背景

随着Web项目的复杂程度逐渐增加,可能会涉及诸如高并发、海量数据查询的的业务场景也逐渐增多;若频繁的操作数据库,会触发数据库的I/O瓶颈,因此需要加入缓存,尽量减少直接操作数据库的频率和次数;同时在分布式系统中,分布式锁等应用场景也需要依赖redis等缓存数据库;redis作为nosql数据库的代表,拥有广泛的应用场景;

这里介绍下Spring集成redis,实现缓存:

准备工作:

1.引入依赖;

<!-- jedis 客户端 -->
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.8.2</version>
</dependency>
<!-- spring data redis -->
<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-redis</artifactId>
    <version>1.7.11.RELEASE</version>
</dependency>

2.定义 redis.proerties 文件,配置连接池相关参数;

# redis configuration

# 连接池配置
redis.maxTotal=30
redis.maxIdle=10
redis.softMinEvictableIdleTimeMillis=10000
redis.blockWhenExhausted=true
redis.maxWaitMillis=1500
redis.testOnBorrow=false

redis.testWhileIdle=true
redis.timeBetweenEvictionRunsMillis=30000
redis.numTestsPerEvictionRun=1024
redis.minEvictableIdleTimeMillis=180000

# 单机配置
redis.host=169.254.244.131
redis.port=6379
# 自己选择是否开启密码
redis.password=123456

# 集群配置
redis.host1=169.254.244.131
redis.port1=7001
redis.host2=169.254.244.131
redis.port2=7002
redis.host3=169.254.244.131
redis.port3=7003
redis.host4=169.254.244.131
redis.port4=7004
redis.host5=169.254.244.131
redis.port5=7005
redis.host6=169.254.244.131
redis.port6=7006

3.连接池配置引入spring上下文中;

<!--加载属性文件-->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:redis.properties</value>
        </list>
    </property>
    <!-- 在多个spring配置文件中都配置了属性文件加载 必须设置为true -->
    <property name="ignoreUnresolvablePlaceholders" value="true"/>
</bean>
单机版
<?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:aop="http://www.springframework.org/schema/aop"
	   xmlns:tx="http://www.springframework.org/schema/tx"
	   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.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd 
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd 
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

	<!-- 连接池配置  -->
	<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
		<!-- 最大连接数 -->
		<property name="maxTotal" value="${redis.maxTotal}" />
		<!-- 最大空闲连接数 -->
		<property name="maxIdle" value="${redis.maxIdle}" />
		<!-- 连接空闲多久后释放, 当空闲时间>该值且空闲连接>最大空闲连接数时直接释放 -->
		<property name="softMinEvictableIdleTimeMillis" value=&
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

暴走编程

您的鼓励将是我创作最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值