• -------------------------------------------------------------
  • ====================================

SpringBoot集成Redis来实现缓存技术方案

建站指南 dewbay 5年前 (2019-04-12) 1689次浏览 已收录 0个评论 扫描二维码

概述

在我们的日常项目开发过程中缓存是无处不在的,因为它可以极大的提高系统的访问速度,关于缓存的框架也种类繁多,今天主要介绍的是使用现在非常流行的 NoSQL 数据库(Redis)来实现我们的缓存需求。

Redis简介

Redis 是一个开源(BSD 许可)的,内存中的数据结构存储系统,它可以用作数据库、缓存和消息中间件,Redis 的优势包括它的速度、支持丰富的数据类型、操作原子性,以及它的通用性。

案例整合

本案例是在之前一篇SpringBoot + Mybatis + RESTful 的基础上来集成 Redis 的,所以大家如有什么不明白的地方可以前往https://my.oschina.net/feinik/blog/879266,由于篇幅原因这里不一一贴出所有的代码,具体完整案例代码可以看这里:https://github.com/AIFEINIK/SpringBoot-Learn/tree/master/spring-boot-redis2,关于 Redis 如何安装可自行 google。

1、在 Maven pom.xml 文件中加入 Redis 包

123456<!--redis--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-redis</artifactId><version>${boot.version}</version></dependency>

2、SpringBoot配置文件中配置 Redis 连接(YAML 方式配置)

123456789101112131415spring:application:name: spring-boot-redisredis:host: 192.168.145.132port: 6379timeout: 20000cluster:nodes: 192.168.211.134:7000,192.168.211.134:7001,192.168.211.134:7002maxRedirects: 6pool:max-active: 8min-idle: 0max-idle: 8max-wait: -1

解释:本配置采用 Redis 一主三从的的配置方式来提高缓存的吞吐量

3、Redis 配置类

1234567891011121314151617181920212223@Configurationpublic class RedisConfig { @Beanpublic RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) {RedisTemplate<Object, Object> template = new RedisTemplate<>();template.setConnectionFactory(connectionFactory); //使用 Jackson2JsonRedisSerializer 来序列化和反序列化 redis 的 value 值Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer(Object.class); ObjectMapper mapper = new ObjectMapper();mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);serializer.setObjectMapper(mapper); template.setValueSerializer(serializer);//使用 StringRedisSerializer 来序列化和反序列化 redis 的 key 值template.setKeySerializer(new StringRedisSerializer());template.afterPropertiesSet();return template;}}

解释:SpringBoot提供了对 Redis 的自动配置功能,在 RedisAutoConfiguration 中默认为我们配置了 JedisConnectionFactory(客户端连接)、RedisTemplate 以及 StringRedisTemplate(数据操作模板),其中 StringRedisTemplate 模板只针对键值对都是字符型的数据进行操作,本示例采用 RedisTemplate 作为数据操作模板,该模板默认采用 JdkSerializationRedisSerializer 的二进制数据序列化方式,为了方便演示本示例采用 Jackson2JsonRedisSerializer 来序列化和反序列化 redis 的 value 值,使用 StringRedisSerializer 来序列化和反序列化 redis 的 key 值。

4、Service 层应用缓存(注解方式)

123456789101112131415161718192021222324252627282930313233343536373839404142@Servicepublic class PersonService { @Autowiredprivate PersonRepo personRepo; /*** @Cacheable 应用到读取数据的方法上,先从缓存中读取,如果没有再从 DB 获取数据,然后把数据添加到缓存中* unless 表示条件表达式成立的话不放入缓存* @param username* @return*/@Cacheable(value = "user", key = "#root.targetClass + #username", unless = "#result eq null")public Person getPersonByName(String username) {Person person = personRepo.getPersonByName(username);return person;} /*** @CachePut 应用到写数据的方法上,如新增/修改方法,调用方法时会自动把相应的数据放入缓存* @param person* @return*/@CachePut(value = "user", key = "#root.targetClass + #result.username", unless = "#person eq null")public Person savePerson(Person person) {return personRepo.savePerson(person);} /*** @CacheEvict 应用到删除数据的方法上,调用方法时会从缓存中删除对应 key 的数据* @param username* @return*/@CacheEvict(value = "user", key = "#root.targetClass + #username", condition = "#result eq true")public boolean removePersonByName(String username) {return personRepo.removePersonByName(username) > 0;} public boolean isExistPersonName(Person person) {return personRepo.existPersonName(person) > 0;}}

解释:

1、这里的缓存 key 为简单的字符串组合,也可根据具体需要实现自定义的 Key 生成器,然后在注解中使用 keyGenerator 来引用。

2、Spring Cache 提供了一些供我们使用的 SpEL 上下文数据,通过#来引用,具体可查看 Spring 官网:http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#cache-spel-context。

5、数据访问资源类

1234567891011121314151617181920212223242526272829303132333435363738@Component@Path("personMgr")public class PersonMgrResource { @Autowiredprivate PersonService personService; @GET@Path("getPersonByName")@Produces(MediaType.APPLICATION_JSON)public JsonResp getPersonByName(@QueryParam("username") String username) {Person person = personService.getPersonByName(username);return JsonResp.success(person);} @POST@Path("removePersonByName")@Produces(MediaType.APPLICATION_JSON)public JsonResp removePersonByName(@QueryParam("username") String username) {if (personService.removePersonByName(username)) {return JsonResp.success();}return JsonResp.fail("系统错误!");} @POST@Path("savePerson")@Produces(MediaType.APPLICATION_JSON)public JsonResp savePerson(Person person) {if (personService.isExistPersonName(person)) {return JsonResp.fail("用户名已存在!");}if (personService.savePerson(person).getId() > 0) {return JsonResp.success();}return JsonResp.fail("系统错误!");}}

6、通过 postman 工具来测试缓存是否生效

第一次访问查找用户:

第一次通过用户名称来查找用户可以看到是从库中查询的数据,我们可以通过 RedisClient 工具来查看数据已放入了缓存

第二次查找用户:发现服务端并未打印任何数据库查询日志,可以知道第二次查询是从缓存中查询得到的数据。

总结

本文介绍如何通过SpringBoot来一步步集成 Redis缓存,关于 Redis 的使用它不仅可以用作缓存,还可以用来构建队列系统,Pub/Sub 实时消息系统,分布式系统的的计数器应用,关于 Redis 更多的介绍,请前往查阅官方文档。


露水湾 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:SpringBoot集成Redis来实现缓存技术方案
喜欢 (0)
[]
分享 (0)
关于作者:
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址