spring-boot-route(十二)整合redis做为缓存

redis简介

redis作为一种非关系型数据库,读写非常快,应用十分广泛,它采用key-value的形式存储数据,value常用的五大数据类型有string(字符串),list(链表),set(集合),zset(有序集合)和hash(哈希表)。

redis的特性决定了它的功能,它可以用来做以下这些事情!

  1. 排行榜,利用zset可以方便的实现排序功能
  2. 计数器,利用redis中原子性的自增操作,可以统计到阅读量,点赞量等功能
  3. 简单消息队列,list存储结构,满足先进先出的原则,可以使用lpush/rpop或rpush/lpop实现简单消息队列
  4. session共享,分布式系统中,可以利用redis实现session共享。spring官方提供的分布式解决方案Spring Session就是利用redis 实现的。

Spring Boot对redis也实现自动化装配,使用非常方便。

Spring Boot整合redis

1. 引入redis依赖

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
    <dependency>
   		<groupId>org.apache.commons</groupId>
    	<artifactId>commons-pool2</artifactId>
    </dependency>
</dependencies>

2. 配置redis相关信息

spring:
  redis:
    # redis库
    database: 0
    # redis 服务器地址
    host: localhost
    # redis 端口号
    port: 6379
    # redis 密码
    password:
    # 连接超时时间(毫秒)
    timeout: 1000
    lettuce:
      pool:
        # 连接池最大链接数(负数表示没有限制)
        max-active: 8
        # 连接池最大阻塞等待时间(负数表示没有限制)
        max-wait: -1
        # 连接池最大空闲连接数
        max-idle: 8
        # 连接池最小空闲连接数
        min-idle: 0

3. 操作redis

SpringBoot提供了两个bean来操作redis,分别是RedisTemplateStringRedisTemplate,这两者的主要区别如下。

RedisTemplate使用的是JdkSerializationRedisSerializer 存入数据会将数据先序列化成字节数组然后在存入Redis数据库。

StringRedisTemplate使用的是StringRedisSerializer。

下面一起来看看效果:

@RestController
public class RedisDemo {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @GetMapping("redisTmeplateData")
    public void redisTemplateData(){

        redisTemplate.opsForValue().set("name","Java旅途");
    }

    @GetMapping("stringRedisTemplateData")
    public void stringRedisTemplateData(){

        stringRedisTemplate.opsForValue().set("desc","坚持分享java技术栈");
    }
}

第一个方法存入的数据如下图

第二个方法存入的数据如下图

由于RedisTemplate是序列化成字节数组存储的,因此在redis客户端的可读性并不好。

自动缓存

@Cacheable可以标记在一个方法上,也可以标记在一个类上。当标记在一个方法上时表示该方法是支持缓存的,当标记在一个类上时则表示该类所有的方法都是支持缓存的。

如果添加了@Cacheable注解,那么方法被调用后,值会被存入redis,下次再调用的时候会直接从redis中取值返回。

@GetMapping("getStudent")
@Cacheable(value = "student:key")
public Student getStudent(){
    log.info("我不是缓存,我是new的对象!");
    Student student = new Student("Java旅途",26);
    return student;
}

记得要开启缓存,在启动类加上@EnableCaching注解

访问上面的方法,如果不打印日志,则是从缓存中获取的值。

封装redisUtils

RedisTemplate提供了很多方法来操作redis,但是找起来比较费事,为了更好的操作redis,一般会封装redisUtils来满足业务开发。这里简单封装几个做个示例,如果开发中有需求可以自己封装。

public class RedisUtils {

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 普通存入
     * @param key
     * @param value
     * @return
     */
    public boolean set(String key,Object value){
        try {
            redisTemplate.opsForValue().set(key,value);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 普通获取key
     * @param key
     * @return
     */
    public Object get(String key){
        return key == null ? null : redisTemplate.opsForValue().get(key);
    }

    /**
     * 存入key,设置过期时长
     * @param key
     * @param value
     * @param time
     * @return
     */
    public boolean set(String key,Object value,long time){
        try {
            if(time > 0){
                redisTemplate.opsForValue().set(key,value,time, TimeUnit.SECONDS);
            }else{
                redisTemplate.opsForValue().set(key,value);
            }
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 判断key是否存在
     * @param key
     * @return
     */
    public boolean exists(String key){
        try {
            return redisTemplate.hasKey(key);
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 删除key
     * @param key
     */
    public void del(String key){
        try {
            if(key != null && key.length() > 0){
                redisTemplate.delete(key);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

本文示例代码已上传至github,点个star支持一下!

Spring Boot系列教程目录

spring-boot-route(一)Controller接收参数的几种方式

spring-boot-route(二)读取配置文件的几种方式

spring-boot-route(三)实现多文件上传

spring-boot-route(四)全局异常处理

spring-boot-route(五)整合swagger生成接口文档

spring-boot-route(六)整合JApiDocs生成接口文档

spring-boot-route(七)整合jdbcTemplate操作数据库

spring-boot-route(八)整合mybatis操作数据库

spring-boot-route(九)整合JPA操作数据库

spring-boot-route(十)多数据源切换

spring-boot-route(十一)数据库配置信息加密

spring-boot-route(十二)整合redis做为缓存

spring-boot-route(十三)整合RabbitMQ

spring-boot-route(十四)整合Kafka

spring-boot-route(十五)整合RocketMQ

spring-boot-route(十六)使用logback生产日志文件

spring-boot-route(十七)使用aop记录操作日志

spring-boot-route(十八)spring-boot-adtuator监控应用

spring-boot-route(十九)spring-boot-admin监控服务

spring-boot-route(二十)Spring Task实现简单定时任务

spring-boot-route(二十一)quartz实现动态定时任务

spring-boot-route(二十二)实现邮件发送功能

spring-boot-route(二十三)开发微信公众号

spring-boot-route(二十四)分布式session的一致性处理

spring-boot-route(二十五)两行代码实现国际化

spring-boot-route(二十六)整合webSocket

这个系列的文章都是工作中频繁用到的知识,学完这个系列,应付日常开发绰绰有余。如果还想了解其他内容,扫面下方二维码告诉我,我会进一步完善这个系列的文章!

posted @ 2020-10-11 11:59  Java旅途  阅读(587)  评论(0编辑  收藏  举报