8.3_springboot+spring data redis

首先是依赖

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

然后配置文件

spring.redis.port=6379
spring.redis.host=localhost

在测试中写代码

需要StringRedisTemplate的对象,直接注入即可

package com.example;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@SpringBootTest
class DemoApplicationTests {


    @Autowired
    private StringRedisTemplate redisTemplate;

    @Test
    public void test() {
        /*一般的 key value 形式*/
        redisTemplate.opsForValue().set("学生2", "李四2");
        System.out.println(redisTemplate.opsForValue().get("学生2"));

        /* 一般的 key value 形式 + 过期时间 */
        redisTemplate.opsForValue().set("日期2", "2022-2年", 10, TimeUnit.SECONDS);
        System.out.println(redisTemplate.opsForValue().get("日期2"));

        redisTemplate.delete("学生2");


    }

    @Test
    public void test1() {
        redisTemplate.opsForList().leftPush("list1", "lisi1");
        redisTemplate.opsForList().leftPush("list1", "wangwu1");
        redisTemplate.opsForList().leftPush("list1", "zhangsan1");

        List<String> list = redisTemplate.opsForList().range("list1", 0, 2);
        if (list != null) {
            list.forEach(a -> System.out.println(a));
        }

    }

    @Test
    public void test2() {
        redisTemplate.opsForHash().put("table1", "key1", "value1");
        redisTemplate.opsForHash().put("table1", "key2", "value2");
        redisTemplate.opsForHash().put("table1", "key3", "value3");
        redisTemplate.opsForHash().put("table1", "key3", "value3");

        Object o = redisTemplate.opsForHash().get("table1", "key2");
        System.out.println("o = " + o);

    }

    @Test
    public void test3() {

        List<String> stringList = new ArrayList<>();
        stringList.add("1");
        stringList.add("2");
        stringList.add("3");
        stringList.add("4");
        stringList.add("5");
        stringList.add("6");
        stringList.add("7");
        stringList.add("8");
        stringList.add("9");
        stringList.add("10");
        stringList.add("10");
        stringList.add("10");

        for (String s : stringList) {
            redisTemplate.opsForSet().add("set", s);
        }

        for (int i = 0; i < 5; i++) {
            /*pop 的时候 好像是 随机的去 删除掉一个*/
            String set = redisTemplate.opsForSet().pop("set");
            System.out.println("set = " + set);
        }

    }

    @Test
    public void test4() {

        Random random=new Random();
        Random random1=new Random();
        for (int i = 0; i < 10; i++) {
            int i1 = random.nextInt(100);
            int i2 = random1.nextInt(100);
            redisTemplate.opsForZSet().add("zset",String.valueOf(i1),i2);
        }

        /*从 score 小的开始取 并输出他们对应的 value */
        Set<String> zset = redisTemplate.opsForZSet().range("zset", 0, 3);
        for (String s : zset) {
            System.out.println(s);
            
        }

    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值