基本
配置
- pom.xml
<!-- redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
- 配置文件
spring:
redis:
host: 192.168.199.132
port: 6375
2.0以后,全都用lettuce
,可看RedisConnectionFactory接口的两个实现类,jedis有些类甚至没导入
说明
- SpringBoot操作数据:spring-data-- jpa jdbc mongodb redis
- SpringData和Boot齐名的项目
在Boot2.x后,原来的jedis被替换为:lettuce
- jedis lettuce
jedis:采用直连,多个线程操作的话,是不安全的,如果想要避免不安全的,使用jedis pool连接池,BIO模式
lettuce:采用netty,实例可以在多个线程中进行共享,不存在线程不安全的情况,可以减少线程数据了,更像NIO模式
源码
- RedisAutoConfiguration
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(RedisOperations.class)
@EnableConfigurationProperties(RedisProperties.class)
@Import({ LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class })
public class RedisAutoConfiguration {
@Bean
@ConditionalOnMissingBean(name = "redisTemplate")
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory)
throws UnknownHostException {
//默认的redisTemplate没有过多的设置,redis对象都是需要序列化!(Netty,NIO,DUBBO,都需要序列化)
//两个泛型都是object,object类型,后使用需要强制转换<String,Object>
RedisTemplate<Object, Object> template = new RedisTemplate<>();
template.setConnectionFactory(redisConnectionFactory);
return template;
}
@Bean
@ConditionalOnMissingBean // TODO: 由于String 是redis中最常用的类型,所以单独提出来一个bean
public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory)
throws UnknownHostException {
StringRedisTemplate template = new StringRedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
return template;
}
}
序列化
- RedisTemplate的源码
@SuppressWarnings("rawtypes") private @Nullable RedisSerializer keySerializer = null;
@SuppressWarnings("rawtypes") private @Nullable RedisSerializer valueSerializer = null;
@SuppressWarnings("rawtypes") private @Nullable RedisSerializer hashKeySerializer = null;
@SuppressWarnings("rawtypes") private @Nullable RedisSerializer hashValueSerializer = null;
- 默认是用Jdk序列化
RedisConfig
/**
* @Author: XF-DD
* @Date: 20/05/22 18:36
*/
@Configuration
public class RedisConfig {
@Bean
@SuppressWarnings("all")
public RedisTemplate<String , Object> redisTemplate(RedisConnectionFactory redisConnectionFactory)
throws UnknownHostException {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(redisConnectionFactory);
//Jackson 的序列化配置
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper mapper = new ObjectMapper();
mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
// mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
// mapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL);
mapper.activateDefaultTyping(mapper.getPolymorphicTypeValidator());
jackson2JsonRedisSerializer.setObjectMapper(mapper);
//String 的序列化
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
//key采用 String 的序列化
template.setKeySerializer(stringRedisSerializer);
//hash的key采用 String 的序列化
template.setHashKeySerializer(stringRedisSerializer);
//value采用 jackson 的序列化
template.setValueSerializer(jackson2JsonRedisSerializer);
//hash的value采用 jackson 的序列化
template.setHashValueSerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet();
return template;
}
@Bean
public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory)
throws UnknownHostException {
StringRedisTemplate template = new StringRedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
return template;
}
}
操作
/**
* redisTemplate.opsFor
* opsFor : 操作
* 1. 五大数据类型
* opsForValue():操作字符串,类似String
* opsForList():操作list,类似list
* opsForHash():
* opsForZSet():
* opsForGeo()
*/
redisTemplate.opsForValue().set("k1","v1");
System.out.println(redisTemplate.opsForValue().get("k1"));
/**
* 2.常用方法可以直接使用
* 事务和基本的CRUD
*/
redisTemplate.expire("k1",1, TimeUnit.SECONDS);
redisTemplate.multi();
redisTemplate.exec();
/**
* 3. 获取connection对象对数据库
*/
RedisConnection connection = redisTemplate.getConnectionFactory().getConnection();
connection.flushDb();
connection.flushAll();