SpringBoot 开启Redis缓存及使用方法

目录
  • redis缓存
  • 主要步骤
  • 具体实践
    • 整体目录结构
    • yml文件里配置redis集群
    • 设置序列化的bean
    • 编写业务controller
    • 关于缓存的其他注解
  • 检验结果

    之前不是说过redis可以当作缓存用嘛
    现在我们就配置一下springboot使用redis的缓存

    redis缓存

    为什么用redis作缓存
    用redis做缓存,是因为redis有着很优秀的读写能力,在集群下可以保证数据的高可用

    主要步骤

     1、pom.xml文件添加依赖

    2、yml文件配置redis集群

    3、编写redisconfig配置序列化及缓存配置,添加缓存注解

    4、编写业务controller,添加缓存注解

    5、编写启动类

    具体实践

    整体目录结构

    pom.xml添加依赖

    <?xml version="1.0" encoding="utf-8"?>
    <project xmlns="http://maven.apache.org/pom/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
             xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelversion>4.0.0</modelversion>
    
        <groupid>org.example</groupid>
        <artifactid>springboot_redis</artifactid>
        <version>1.0-snapshot</version>
    
        <properties>
            <java.version>1.8</java.version>
        </properties>
        <parent>
            <groupid>org.springframework.boot</groupid>
            <artifactid>spring-boot-starter-parent</artifactid>
            <version>2.1.8.release</version>
        </parent>
    
        <dependencies>
            <dependency>
                <groupid>org.springframework.boot</groupid>
                <artifactid>spring-boot-starter-web</artifactid>
            </dependency>
            <!--整合redis-->
            <dependency>
                <groupid>org.springframework.boot</groupid>
                <artifactid>spring-boot-starter-data-redis</artifactid>
            </dependency>
            <!--spring boot test-->
            <dependency>
                <groupid>org.springframework.boot</groupid>
                <artifactid>spring-boot-starter-test</artifactid>
            </dependency>
        </dependencies>
    
    </project>

    yml文件里配置redis集群

    结构是ip+port

    spring:
      redis:
        cluster:
          nodes:
            - 169.254.0.100:8001
            - 169.254.0.100:8002
            - 169.254.0.100:8003
            - 169.254.0.100:8004
            - 169.254.0.100:8005
            - 169.254.0.100:8006

    编写redisconfig配置序列化及缓存配置,添加缓存注解

    设置序列化的bean

    设置缓存的bean

    这里有必要解释一下

    • cachenames.add() 这里我理解的是和controller进行绑定,毕竟很多controller的时候,这里可以确定到底那个controller开启缓存,以及每个controller对缓存的要求可能也不一样
    • configmap这里就是将我们对缓存的一些配置和命名空间进行关联
    • 设置缓存时间和禁止缓存空数据应该还好理解

    编写业务controller

    @restcontroller
    @requestmapping("user")
    public class rediscachecontroller {
        @cacheable(value = "user",key = "#root.methodname+#root.args[0]")
        @getmapping("findword/{id}")
        public string findword(@pathvariable string id) {
            system.out.println("cacheing");
            hashmap<string, string> words = new hashmap<>();
            words.put("1", "java");
            words.put("2", "redis");
            words.put("3", "cache");
            return words.get(id);
        }
    }

    @cacheable一定要加在方法之上
    value就是之前在redisconfig中定义的命名空间,也是缓存保存的空间
    key就是缓存保存的key,这里以方法名为key,但是为避免方法名重复导致的key重复,所以加入id,来避免重复

    关于缓存的其他注解

    • @cacheput

    在支持spring cache的环境中,对于使用@cacheale标注的方法,spring在每次执行前都会检查cache中是否存在相同的key的缓存元素,如果存在就不再执行该方法,而是从缓存中获取结果直接进行返回,若不存在才会执行方法并将返回结果存入指定的缓存中

    @cacheput也可以生命一个方法支持缓存功能,与@cacheable不同的是使用@cacheput标注的方法在执行并不会去检查缓存中是否存在之前执行过的结果,而是每次都会执行该方法,并将执行结果以键值对的形式存入缓存中。

    • @cacheevict

    cacheevict是用来标注在需要清除缓存元素的方法或类上的。
    当标记在一个类上时表示其中所有的方法的执行都会触发缓存的清除操作
    @cacheevict可以指定的属性有value、key、condition、allentries和beforeinvocation。
    其中value、key和condition的语义与@cacheable对应的属性类似。即value表示清除操作是发生在哪些缓存(命名空间)上的
    key表示要清除的是哪个key,如未指定则会谁用默认策略生成的key,condition表示清除操作发生的条件
    allentries属性
    allentries是boolean类型的,表示是否要清除缓存中的所有元素,默认为false,当指定为true时,会忽略指定的key
    beforeinvocation属性
    清除操作默认时在对应方法成功执行后触发的,即方法如果因为抛出异常而未能成功返回也不会触发清除操作
    使用beforeinvocation可以改变触发清除操作的时间,当我们指定属性值为true时,spring会在调用该方法之前清除缓存中的指定元素 编写启动类

    就是传统的启动类

    检验结果

    使用postman发送请求进行检测
    第一次的时候可以看到控制台打印

    这说明方法执行了
    但是第二次发送相同请求的时候,可以看到拿到了数据,但是方法没有执行,说明缓存有用了

    好了,到此结束。

    到此这篇关于springboot 开启redis缓存的文章就介绍到这了,更多相关springboot redis缓存内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!

    (0)
    上一篇 2022年3月21日
    下一篇 2022年3月21日

    相关推荐