SpringBoot2.0 开始默认是使用 lettuce [ˈletɪs] 连接池来连接 Redis 的,这也就意味着在配置 Maven 依赖的时候需要额外注意,不要引入 Jedis 的依赖了!本篇采用默认 lettuce 方式。
连接配置
一、Maven 依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-tcnative</artifactId>
<version>2.0.5.Final</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-handler</artifactId>
<version>4.0.27.Final</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.6.2</version>
</dependency>
二、配置文件
##redis
#服务器地址
spring.redis.host=127.0.0.1
#端口
spring.redis.port=6379
#数据库号
spring.redis.database=0
#密码,默认为空
spring.redis.password=foobared
#连接池最大空闲连接数,默认8
spring.redis.lettuce.pool.max-idle=8
#连接池最小空闲连接数,默认0
spring.redis.lettuce.pool.min-idle=0
#连接超时时间(ms)
spring.redis.timeout=3000
以上两步配置好后,启动 Redis 服务端,就可以连接上 Redis 数据库。下面是操作 Redis 步骤。
三、Redis 封装工具类
将 Redis 封装成工具类,方便后续使用操作。下面提供给个简单思路。
@Component
public final class RedisUtils {
@Autowired
private RedisTemplate redisTemplate;
/**
*@Description: key 值是否存在
*
*@Param: [key]
*@Return: java.lang.Boolean
*@Author: fsyml
*@Date 2021/8/25
*/
public Boolean hasKey(String key){
return redisTemplate.hasKey(key);
}
/**
*@Description: key 删除
*
*@Param: [key]
*@Return: java.lang.Boolean
*@Author: fsyml
*@Date 2021/8/25
*/
public Boolean deleteKey(String key){
return redisTemplate.delete(key);
}
/**
*@Description: 哈希是否存在
*
*@Param: [key, hmKey]
*@Return: java.lang.Boolean
*@Author: fsyml
*@Date 2021/8/25
*/
public Boolean hasKeyMap(String key, String hmKey){
boolean result = true;
if(redisTemplate.hasKey(key)){
result = redisTemplate.opsForHash().hasKey(key, hmKey);
}else {
result = false;
}
return result;
}
/**
*@Description: 哈希插入值
*
*@Param: [key, hmKey, object]
*@Return: java.lang.Boolean
*@Author: fsyml
*@Date 2021/8/25
*/
public Boolean putHashMap(String key, String hmKey, Object object){
boolean result = true;
try{
redisTemplate.opsForHash().put(key, hmKey, JSONArray.toJSON(object));
}
catch (Exception e){
e.printStackTrace();
result = false;
}
return result;
}
....
}
四、调用 RedisUtils 工具类
@Autowired
private RedisUtils redisUtils;
@Override
public Boolean hasUser(String userCode) {
return redisUtils.hasKeyMap("USER_INFO_KEY", userCode);
}
//用户信息存入 Redis
@Override
public void addUser(UserCache userCache) {
if(userCache != null){
redisUtils.putHashMap("USER_INFO_KEY", userCache.getUserCode(), userCache);
}
}
上面提供的是业务层调用 封装的 RedisUtils 工具类的两个简单方法,没有贴出大篇幅的代码,相信应该能够初步说明怎样操作 Redis。
五、补充记录几点
1、Redis 中的数据显示的 16 进制序列
如下图所示:
出现了以上问题时,需要配置 Redis 的序列化方式。新增 Redis 配置类:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
* @Discription redis 配置
*
* @Author fsyml
* @Date 2021/8/30
* @Modified By:
*/
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory){
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(factory);
//key 序列化方式为 String
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
redisTemplate.setKeySerializer(stringRedisSerializer);
redisTemplate.setHashKeySerializer(stringRedisSerializer);
//value 序列化方式为 JSON
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
return redisTemplate;
}
}
重新启动项目,看看效果:
2、给 Redis 配置连接密码
编辑 redis.windows-service.conf 找到 requirepass 取消注释,密码内容按需修改。
################################## SECURITY ###################################
# Require clients to issue AUTH <PASSWORD> before processing any other
# commands. This might be useful in environments in which you do not trust
# others with access to the host running redis-server.
#
# This should stay commented out for backward compatibility and because most
# people do not need auth (e.g. they run their own servers).
#
# Warning: since Redis is pretty fast an outside user can try up to
# 150k passwords per second against a good box. This means that you should
# use a very strong password otherwise it will be very easy to break.
#
requirepass foobared
配置连接密码后,启动服务需要加上配置文件。(本篇 Redis 为 window 环境)
D:\setup\Redis-x64-5.0.10>redis-server.exe redis.windows-service.conf