在 Redis 出现以前,咱们的缓存框架各类各样,有了 Redis ,缓存方案基本上都统一了,关于 Redis,松哥以前有一个系列教程,尚不了解 Redis 的小伙伴能够参考这个教程:java
使用 Java 操做 Redis 的方案不少,Jedis 是目前较为流行的一种方案,除了 Jedis ,还有不少其余解决方案,以下:web
除了这些方案以外,还有一个使用也至关多的方案,就是 Spring Data Redis。redis
在传统的 SSM 中,须要开发者本身来配置 Spring Data Redis ,这个配置比较繁琐,主要配置 3 个东西:链接池、链接器信息以及 key 和 value 的序列化方案。spring
在 Spring Boot 中,默认集成的 Redis 就是 Spring Data Redis,默认底层的链接池使用了 lettuce ,开发者能够自行修改成本身的熟悉的,例如 Jedis。apache
Spring Data Redis 针对 Redis 提供了很是方便的操做模板 RedisTemplate 。这是 Spring Data 擅长的事情,那么接下来咱们就来看看 Spring Boot 中 Spring Data Redis 的具体用法。后端
建立工程,引入 Redis 依赖:缓存
建立成功后,还须要手动引入 commos-pool2 的依赖,所以最终完整的 pom.xml 依赖以下:app
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
复制代码
这里主要就是引入了 Spring Data Redis + 链接池。框架
接下来配置 Redis 的信息,信息包含两方面,一方面是 Redis 的基本信息,另外一方面则是链接池信息:前后端分离
spring.redis.database=0
spring.redis.password=123
spring.redis.port=6379
spring.redis.host=192.168.66.128
spring.redis.lettuce.pool.min-idle=5
spring.redis.lettuce.pool.max-idle=10
spring.redis.lettuce.pool.max-active=8
spring.redis.lettuce.pool.max-wait=1ms
spring.redis.lettuce.shutdown-timeout=100ms
复制代码
当开发者在项目中引入了 Spring Data Redis ,而且配置了 Redis 的基本信息,此时,自动化配置就会生效。
咱们从 Spring Boot 中 Redis 的自动化配置类中就能够看出端倪:
@Configuration
@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<Object, Object> template = new RedisTemplate<>();
template.setConnectionFactory(redisConnectionFactory);
return template;
}
@Bean
@ConditionalOnMissingBean
public StringRedisTemplate stringRedisTemplate( RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
StringRedisTemplate template = new StringRedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
return template;
}
}
复制代码
这个自动化配置类很好理解:
接下来,能够直接在 Service 中注入 StringRedisTemplate 或者 RedisTemplate 来使用:
@Service
public class HelloService {
@Autowired
RedisTemplate redisTemplate;
public void hello() {
ValueOperations ops = redisTemplate.opsForValue();
ops.set("k1", "v1");
Object k1 = ops.get("k1");
System.out.println(k1);
}
}
复制代码
Redis 中的数据操做,大致上来讲,能够分为两种:
调用该方法就能够将数据存储到 Redis 中去了,以下:
k1 前面的字符是因为使用了 RedisTemplate 致使的,RedisTemplate 对 key 进行序列化以后的结果。
RedisTemplate 中,key 默认的序列化方案是 JdkSerializationRedisSerializer 。
而在 StringRedisTemplate 中,key 默认的序列化方案是 StringRedisSerializer ,所以,若是使用 StringRedisTemplate ,默认状况下 key 前面不会有前缀。
不过开发者也能够自行修改 RedisTemplate 中的序列化方案,以下:
@Service
public class HelloService {
@Autowired
RedisTemplate redisTemplate;
public void hello() {
redisTemplate.setKeySerializer(new StringRedisSerializer());
ValueOperations ops = redisTemplate.opsForValue();
ops.set("k1", "v1");
Object k1 = ops.get("k1");
System.out.println(k1);
}
}
复制代码
固然也能够直接使用 StringRedisTemplate:
@Service
public class HelloService {
@Autowired
StringRedisTemplate stringRedisTemplate;
public void hello2() {
ValueOperations ops = stringRedisTemplate.opsForValue();
ops.set("k2", "v2");
Object k1 = ops.get("k2");
System.out.println(k1);
}
}
复制代码
另外须要注意 ,Spring Boot 的自动化配置,只能配置单机的 Redis ,若是是 Redis 集群,则全部的东西都须要本身手动配置,关于如何操做 Redis 集群,松哥之后再来和你们分享。
经过 Spring Cache 的形式来操做 Redis,Spring Cache 统一了缓存江湖的门面,这种方案,松哥以前有过一篇专门的文章介绍,小伙伴能够移步这里:Spring Boot中,Redis缓存还能这么用!。
第三种方案,就是直接使用 Jedis 或者 其余的客户端工具来操做 Redis ,这种方案在 Spring Boot 中也是支持的,虽然操做麻烦,可是支持,这种操做松哥以前也有介绍的文章,所以这里就再也不赘述了,能够参考 Jedis 使用。
Spring Boot 中,Redis 的操做,这里松哥给你们总结了三种方案,实际上前两个使用普遍一些,直接使用 Jedis 仍是比较少,基本上 Spring Boot 中没见过有人直接这么搞。
好了,本文就说到这里,有问题欢迎留言讨论。
关注公众号【江南一点雨】,专一于 Spring Boot+微服务以及先后端分离等全栈技术,按期视频教程分享,关注后回复 Java ,领取松哥为你精心准备的 Java 干货!