1,引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.7.18</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.32</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.33</version>
</dependency>
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2</artifactId>
<version>2.0.50</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.14</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.7.18</version>
</dependency>
2,application.yml添加
redis:
cache: false
host: xxxxx # Redis服务器地址
port: 6379 # Redis服务器端口
database: 0 # Redis数据库索引
password: xxxxxx #密码
lettuce:
pool:
max-active: 20 # 连接池最大连接数
max-wait: -1 # 最大阻塞等待时间
max-idle: 5 # 连接池中最大空闲连接
min-idle: 0 # 连接池中最小空闲连接
3,创建RedisUtil.java类
package com.xxxx.util;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
@Component
public class RedisUtil {
@Autowired
private StringRedisTemplate redisTemplate;
// 添加key-value对,并设置过期时间
public void set(String key, String value, Long timeout, TimeUnit timeUnit) {
if (timeout == null || timeUnit == null) {
// 默认过期时间为一天
timeout = 1L;
timeUnit = TimeUnit.DAYS;
}
redisTemplate.opsForValue().set(key, value, timeout, timeUnit);
}
// 添加key-value对,不过期
public void set(String key, String value) {
redisTemplate.opsForValue().set(key, value);
}
// 获取key对应的value
public String get(String key) {
return redisTemplate.opsForValue().get(key);
}
// 修改key的value,并设置过期时间
public void update(String key, String newValue, Long timeout, TimeUnit timeUnit) {
if (timeout == null || timeUnit == null) {
// 默认过期时间为一天
timeout = 1L;
timeUnit = TimeUnit.DAYS;
}
redisTemplate.opsForValue().set(key, newValue, timeout, timeUnit);
}
// 修改key的value,不修改过期时间
public void update(String key, String newValue) {
redisTemplate.opsForValue().set(key, newValue);
}
// 设置key的过期时间
public void expire(String key, Long timeout, TimeUnit timeUnit) {
redisTemplate.expire(key, timeout, timeUnit);
}
// 删除key
public void delete(String key) {
redisTemplate.delete(key);
}
// 从Redis获取字符串并转换为List<T>
public <T> List<T> getStringAsList(String key, Class<T> type) throws IOException {
String json = redisTemplate.opsForValue().get(key);
if (json == null) {
return null;
}
ObjectMapper mapper = new ObjectMapper();
return mapper.readValue(json, mapper.getTypeFactory().constructCollectionType(List.class, type));
}
// 从Redis获取字符串并转换为Map<String, Long>
public <T> Map<String, T> getStringAsMap(String key, Class<T> type) throws IOException {
String json = redisTemplate.opsForValue().get(key);
if (json == null) {
return null;
}
ObjectMapper mapper = new ObjectMapper();
// 使用TypeReference来指定Map的泛型类型
TypeReference<Map<String, T>> typeRef = new TypeReference<Map<String, T>>() {
};
return mapper.readValue(json, typeRef);
}
}
一年之计在于春,一天之计在于晨。