上一篇 【第一章】Centos7.x安装Redis , 以及redis常用命令使用.
Redis简介
redis是一个高性能的键(Key)值(Value)数据库. 是当前最热门的的的NoSql数据库之一.
Redis优势
- 基于内存,速度快
- 数据持久化的支持,可以将内存中的数据异步写入到硬盘中.
- 数据结构丰富
- string<字符串>
- list<链表>
- set<集合>
- zset<有序集合>
- Hash<哈希类型>
SpringBoot2.x集成Redis
SpringBoot1.x中 spring-boot-starter-data-redis 默认客户端是Jedis
SpringBoot2.x中 默认客户端是Lettuce
引入依赖
采用commons-pool2 作为redis连接池
<!-- redis starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- lettuce pool 缓存连接池 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
application.yml配置
spring:
redis:
# Redis数据库索引(默认为0)
database: 0
# Redis服务器连接端口
port: 6379
# Redis服务器地址
host: 127.0.0.1
# Redis服务器连接密码(默认为空)
password: 123456
# 连接超时时间(毫秒)
timeout: 5000
lettuce:
# 关闭超时时间
shutdown-timeout: 100
pool:
# 连接池最大连接数(使用负值表示没有限制)
max-active: 8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
max-wait: 10000
# 连接池中的最大空闲连接
max-idle: 8
# 连接池中的最小空闲连接
min-idle: 0
RedisConfig配置key,value序列化
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
* @Title: RedisCacheConfig
* @Description: TODO(reids缓存配置)
* @Author 爱飘de小子
* @Date 2019/6/5下午 11:36
*/
@Configuration
public class RedisConfig {
/**
* RedisTemplate配置
* 注意: 注入的是LettuceConnectionFactory
*/
@Bean
public RedisTemplate<String,Object> redisTemplate(LettuceConnectionFactory factory){
RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(factory);
redisTemplate.setKeySerializer(keySerializer());
redisTemplate.setHashKeySerializer(keySerializer());
redisTemplate.setValueSerializer(valueSerializer());
redisTemplate.setHashValueSerializer(valueSerializer());
return redisTemplate;
}
/**
* key序列化
*/
private RedisSerializer<String> keySerializer() {
return new StringRedisSerializer();
}
/**
* value序列化
*/
private RedisSerializer<Object> valueSerializer() {
return new GenericJackson2JsonRedisSerializer();
}
}
至此,springboot已经集成redis. 项目中可以使用RedisTemplate泛型类工具类操作redis数据库.
附: redis操作工具类(基于String,list,Map操作)
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
/**
* @Title: RedisService
* @Description: TODO(reids工具类)
* @Author 爱飘de小子
* @Date 2019/6/5
*/
@Service
public class RedisService {
@Autowired
private RedisTemplate<String, Object> redisTemplate;
// ========================&#