spring|springboot使用redis(从配置到实战)

概述springboot通常整合redis采用的是RedisTemplate的形式除了这种形式以外还有另外一种形式去整合即采用spring支持的注解进行访问缓存.
准备工作pom.xml
```
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.7.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
<version>RELEASE</version>
</dependency>
```
application.properties
```
# REDIS (RedisProperties)
# Redis数据库索引(默认为0)
spring.redis.database=0
# Redis服务器地址
spring.redis.host=127.0.0.1
# Redis服务器连接端口
spring.redis.port=6379
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active=8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=-1
# 连接池中的最大空闲连接
spring.redis.pool.max-idle=8
# 连接池中的最小空闲连接
spring.redis.pool.min-idle=0
# 连接超时时间(毫秒)
spring.redis.timeout=0
```
Redis配置类```
package cn.chenlove.config;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport{
@Value(\"${spring.redis.host\")
private String host;
@Value(\"${spring.redis.port\")
private int port;
@Value(\"${spring.redis.timeout\")
private int timeout;
@Value(\"${spring.redis.pool.max-idle\")
private int maxIdle;
@Value(\"${spring.redis.pool.max-wait\")
private long maxWaitMillis;
@Bean
public JedisPool redisPoolFactory() {
Logger.getLogger(getClass()).info(\"JedisPool注入成功!!\");
Logger.getLogger(getClass()).info(\"redis地址:\" + host + \":\" + port);
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxIdle(maxIdle);
jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
JedisPool jedisPool = new JedisPool(jedisPoolConfig host port timeout);
return jedisPool;
```
【spring|springboot使用redis(从配置到实战)】可以看出我们这里主要配置了两个东西cacheManager方法配置了一个缓存名称它的名字叫做thisredis当我们要在方法注解里面使用到它的时候就要根据名称进行区分不同缓存.同时设置了缓
- iPhone|美媒:iPhone15确认使用USB - C口,今年升级有遗憾!
- 中兴|使用中兴Axon40UItra之后,明白屏幕完美,却在市场上遇冷的原因
- 互联互通|哪些无人机适合新手使用?从普宙O2到大疆Mini 3 Pro,一文读懂
- 华为|越来越全面性的手机,过度使用,是否会损坏其内存卡机速度
- 如何实现一根网线连接路由器,既能上网又可以使用itv呢?
- iPhone 13深度使用半年,体验虽然流畅,但两个缺点却让人无奈!
- 电饭锅第一次使用,有什么性能检测法子?
- 如何在 Notion 类产品中使用卡片笔记写作法:理念与实践
- B站“特殊版”来了!全程使用无广告,比开会员还给力
- 马克·扎克伯格|扎克伯格宣布打造元宇宙钱包,计划让10亿人使用元宇宙
