【Java】缓存配置

本文介绍了如何在Java应用中配置Redis缓存,包括引入相关jar包,创建spring-redis.xml配置文件。同时详细讲解了两种使用方式:一是通过注解的方式,在查询时实现数据缓存,增删改操作时自动清除缓存;二是利用模板方式进行缓存操作。

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

引入jar包

<!—引入tool-->
<dependency>
<groupId>com.dmsdbj.itoo</groupId>
<artifactId>itoo-tool</artifactId>
</dependency>
<!--redis#-->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<!—spring-data-redis-->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>2.0.8.RELEASE</version>
</dependency>

创建spring-redis.xml配置文件

<!----------------------------------redis配置单机版--------------------------->
<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd">
<!--启用缓存注解功能-->
<cache:annotation-drivencache-manager="redisCacheManager"/>
<!---redis单机版配置-->
<beanid="jedisConnectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionF
actory">
<constructor-arg>
<bean
class="org.springframework.data.redis.connection.RedisStandaloneConfigu
ration"
c:host-name="192.168.22.64"c:port="6379"/>
</constructor-arg>
</bean>
<beanid="poolConfig"class="redis.clients.jedis.JedisPoolConfig">
<!--最小空闲数-->
<propertyname="minIdle"value="5"/><!--最大空闲数-->
<propertyname="maxIdle"value="100"/><!--最大连接数
-->
<propertyname="maxTotal"value="300"/><!--最大等待时间
单位毫秒(ms)-->
<propertyname="maxWaitMillis"value="3000"/><!--使用连
接时测试连接是否可用-->
<propertyname="testOnBorrow"value="true"/>
</bean>
<!--缓存key和value序列化配置-->
<beanid="redisCacheManager"
class="com.dmsdbj.itoo.tool.redis.TedisCacheManager"
factory-method="create"
c:connection-factory-ref="jedisConnectionFactory">
<propertyname="keySerializer"ref="keySerializer"/>
<propertyname="valueSerializer"ref="valueSerializer"/>
</bean>
<beanid="keySerializer"
class="org.springframework.data.redis.serializer.StringRedisSerializer"
/>
<beanid="valueSerializer"
class="com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializ
er"/>
<!--redis模板-->
<beanid="redisTemplate"
class="org.springframework.data.redis.core.RedisTemplate">
<propertyname="connectionFactory"
ref="jedisConnectionFactory"/>
<propertyname="keySerializer"ref="keySerializer"/>
<propertyname="valueSerializer"ref="valueSerializer"/>
</bean>
<!--Session共享-->
<beanid="redisHttpSessionConfiguration"
class="org.springframework.session.data.redis.config.annotation.web.htt
p.RedisHttpSessionConfiguration">
<propertyname="maxInactiveIntervalInSeconds"value="7200"/>
</bean>
</beans>

使用方式
1.注解方式使用
查询时添加注解进行缓存
增删改时清除缓存
2.模板方式使用

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值