微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

springBoot redis

导入依赖

  <!--springboot工程需要继承的父工程-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.8.RELEASE</version>
    </parent>


    <dependencies>
        <!--    junit-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        
        <!--        redis-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
    </dependencies>

application.yml配置文件

server:
  port: 8000

spring:
  redis:
    host: localhost
    port: 6379

测试

    @Autowired
    private Redistemplate<String,String> redistemplate;

    @Test
    public void TestString(){
        //存储数据
        redistemplate.opsForValue().set("a1","1");
        //查询数据
        String a1 = redistemplate.opsForValue().get("a1");
        System.out.println("查询道德数据"+a1);

        //判断该键是否存在
        Boolean a11 = redistemplate.hasKey("a1");
        System.out.println("该键是否存在"+a11);

        //增加2
        Long a12 = redistemplate.opsForValue().increment("a1", 2);
        System.out.println("增加后的值为"+a12);

        //删除
        Boolean aboolean = redistemplate.delete("a1");
        System.out.println("是否删除"+aboolean);

        //设置时间 1秒
        redistemplate.opsForValue().set("a1","1", 1,TimeUnit.SECONDS);

        String a13="0";
        while (a13!=null){
            a13 = redistemplate.opsForValue().get("a1");
            
        }
        System.out.println("延时过后数据为null");
    }

在这里插入图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐