SpringBoot集成Redis
SpringBoot 操作数据: spring-data jpa jdbc mongodb redis !
SpringData也是和SpringBoot齐名的项目!
说明︰在SpringBoot2.x之后,原来使用的jedis被替换为了lettuce?
jedis:采用的直连,多个线程操作的话,是不安全的,如果想要避免不安全的,使用jedis pool连接池!更像BIO模式
lettuce:采用netty,实例可以再多个线程中进行共享,不存在线程不安全的情况! 可以减少线程数据了,更像 NIO模式
源码分析
public class RedisAutoConfiguration {
@Bean
@ConditionalOnMissingBean(name = "redisTemplate") //我们可以自定义一个RedisTemplate 来替换这个默认的
@ConditionalOnSingleCandidate(RedisConnectionFactory.class)
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
//默认的RedisTemplate 没有过的这只,redis对象都是需要序列化的
// 两个泛型都是 Object, Object 的类型,我们后使用需要强制转换<String, Obkect>
RedisTemplate<Object, Object> template = new RedisTemplate<>();
template.setConnectionFactory(redisConnectionFactory);
return template;
}
@Bean
@ConditionalOnMissingBean //由于String 是人跌势中最常使用的类型,所以说单独提出来了一个bean
@ConditionalOnSingleCandidate(RedisConnectionFactory.class)
public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
StringRedisTemplate template = new StringRedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
return template;
}
}
测试:
需要安装Redis才可以
- 导入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>
- 配置连接
# REDIS (RedisProperties)
spring.redis.host=127.0.0.1
spring.redis.port=6379
# Redis数据库索引(默认为0)
spring.redis.database=0
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.jedis.pool.max-active=8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.jedis.pool.max-wait=-1
# 连接池中的最大空闲连接
spring.redis.jedis.pool.max-idle=8
# 连接池中的最小空闲连接
spring.redis.jedis.pool.min-idle=0
# 连接超时时间(毫秒)
spring.redis.timeout=5000
- 测试!
package com.saxon;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
@SpringBootTest
class SpringbootRedis02ApplicationTests {
@Autowired
private StringRedisTemplate redis1StringRedisTemplate;
@Test
void contextLoads() {
LettuceConnectionFactory jedisConnectionFactory = (LettuceConnectionFactory) redis1StringRedisTemplate
.getConnectionFactory();
redis1StringRedisTemplate.opsForValue().set("key","测试redis");
System.out.println(redis1StringRedisTemplate.opsForValue().get("key"));
}
}
注意:要提前打开redis
SpringDataRedis自定义模板解决序列化问题
RedisConfig.java
package com.cc.springdatademo01.config;
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.StringRedisSerializer;
@Configuration
public class RedisConfig {
@Bean //入参:redis连接工厂
public RedisTemplate<String,Object> redisTemplate(LettuceConnectionFactory lettuceConnectionFactory){
RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>();
// 为string 类型的key设置序列化
redisTemplate.setKeySerializer(new StringRedisSerializer());
// 为string类型的value设置序列化
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
// 五种数据类型用得最多的就是string和hash,也设置一下hash
// 为hash类型的key设置序列化
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
// 为hash类型的value设置序列化
redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
// 把连接工厂放进去
redisTemplate.setConnectionFactory(lettuceConnectionFactory);
return redisTemplate;
}
}