概述

在我们的日常项目开发过程中缓存是无处不在的,因为它可以极大的提高系统的访问速度,关于缓存的框架也种类繁多,今天主要介绍的是使用现在非常流行的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包

1
2
3
4
5
6
<!--redis-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-redis</artifactId>
    <version>${boot.version}</version>
</dependency>

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
spring:
    application:
        name: spring-boot-redis
    redis:
        host: 192.168.145.132
        port: 6379
        timeout: 20000
        cluster:
            nodes: 192.168.211.134:7000,192.168.211.134:7001,192.168.211.134:7002
            maxRedirects: 6
        pool:
            max-active: 8
            min-idle: 0
            max-idle: 8
            max-wait: -1

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

3、Redis配置类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
@Configuration
public class RedisConfig {
 
   @Bean
   public 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层应用缓存(注解方式)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
@Service
public class PersonService {
 
    @Autowired
    private 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、数据访问资源类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
@Component
@Path("personMgr")
public class PersonMgrResource {
 
    @Autowired
    private 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更多的介绍,请前往查阅官方文档。

SpringBoot集成Redis来实现缓存技术方案的更多相关文章

  1. springboot集成redis(mybatis、分布式session)

    安装Redis请参考:<CentOS快速安装Redis> 一.springboot集成redis并实现DB与缓存同步 1.添加redis及数据库相关依赖(pom.xml) <depe ...

  2. 【springBoot】springBoot集成redis的key,value序列化的相关问题

    使用的是maven工程 springBoot集成redis默认使用的是注解,在官方文档中只需要2步; 1.在pom文件中引入即可 <dependency> <groupId>o ...

  3. SpringBoot集成redis的key,value序列化的相关问题

    使用的是maven工程 springBoot集成redis默认使用的是注解,在官方文档中只需要2步; 1.在pom文件中引入即可 <dependency> <groupId>o ...

  4. Windows环境下springboot集成redis的安装与使用

    一,redis安装 首先我们需要下载Windows版本的redis压缩包地址如下: https://github.com/MicrosoftArchive/redis/releases 连接打开后如下 ...

  5. springBoot集成Redis遇到的坑(择库)源码分析为什么择库失败

    提示: springboot提供了一套链接redis的api,也就是个jar包,用到的连接类叫做LettuceConnectionConfiguration,所以我们引入pom时是这样的 <de ...

  6. springboot集成redis使用redis作为session报错ClassNotFoundException类RememberMeServices

    springboot 集成redis使用redis作为缓存,会报错的问题. 错误信息: java.lang.IllegalStateException: Error processing condit ...

  7. SpringBoot | 集成Redis

    Windows下安装: https://github.com/MicrosoftArchive/redis/releases zip下就解包到自定义目录下,msi就跟着步骤安装 进入安装目录下运行命令 ...

  8. SpringBoot集成Redis 一 分布式锁 与 缓存

    1.添加依赖及配置(application.yml) <!-- 引入redis依赖 --> <dependency> <groupId>org.springfram ...

  9. SpringBoot集成Redis分布式锁以及Redis缓存

    https://blog.csdn.net/qq_26525215/article/details/79182687 集成Redis 首先在pom.xml中加入需要的redis依赖和缓存依赖 < ...

随机推荐

  1. CF450B Jzzhu and Sequences(矩阵加速)

    CF450B Jzzhu and Sequences 大佬留言:这.这.不就是矩乘的模板吗,切掉它!! You are given xx and yy , please calculate $f_{n ...

  2. try catch影响Spring事务吗?

    对于这个问题有两种情况: 1.catch只打印异常,不抛出异常 try { 数据库做添加订单表; /; 数据库减少库存; }catch (Exception e){ e.printStackTrace ...

  3. python 读取指定文件信息并拼接

    python 读取指定文本并拼接成指定的格式 # -*- coding: utf-8 -*- import os def getHelloWorld(path, fileName): "&q ...

  4. 洛谷 1712 BZOJ 4653 [NOI2016]区间

    [题解] 先把区间按照未离散化的长度排序,保存区间长度,然后离散化区间端点.每次把区间覆盖的点的覆盖次数加1,如果某个点被覆盖次数大于等于m,就从前往后开始删除区间直到没有一个点被覆盖的次数大于等于m ...

  5. POJ3624 0-1背包(dp+滚动数组)

    Charm Bracelet Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 47440   Accepted: 20178 ...

  6. How Can You Tell the Difference Between LINQ Methods and Query Builder Methods?

    LINQ's method syntax looks very similar to the query builder methods,except for one big difference:t ...

  7. 【Codeforces 479D】Long Jumps

    [链接] 我是链接,点我呀:) [题意] 如果存在a[j]-a[i]=d 那么认为可以量出来长度d 现在给你量尺上的n个点. 问你最少要加多少个点,才能够量出来长度x和长度y [题解] 设dic1和d ...

  8. 九度oj 题目1053:互换最大最小数

    题目1053:互换最大最小数 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:7538 解决:3049 题目描述: 输入一个数n,然后输入n个数值各不相同,调换数组中最大和最小的两个数,然后 ...

  9. [ZJOI2008]杀蚂蚁antbuster

    [ZJOI2008]杀蚂蚁antbuster 题目 最近,佳佳迷上了一款好玩的小游戏:antbuster.游戏规则非常简单:在一张地图上,左上角是蚂蚁窝,右下角是蛋糕,蚂蚁会源源不断地从窝里爬出来,试 ...

  10. hdu poj KMP简单题目总结

    hdu 3336 题意:输入一个字符串求每个前缀在串中出现的次数和 sol:只要稍微理解下next 数组的含义就知道只要把每个有意义的next值得个数加起来即可 PS:网上有dp解法orz,dp[i] ...