其他分享
首页 > 其他分享> > springboot整合reids API整理

springboot整合reids API整理

作者:互联网

package com.zpf.homeworkBy18;

import com.zpf.homeworkBy18.service.ClassUserService;
import com.zpf.homeworkBy18.utils.SchedeludByConfUtil;
import org.junit.jupiter.api.Test;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.connection.RedisZSetCommands;
import org.springframework.data.redis.core.RedisKeyValueTemplate;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@SpringBootTest
class HomeworkApplicationTests {

    @Autowired
    private ClassUserService classUserService;

    @Test
    void contextLoads() {
    }

    @Test
    public  void test()
    {
        Set<String> fileNameByDir = classUserService.getFileNameByDir();
        Set<String> nameByExcelFile = classUserService.getNameByExcelFile();
        nameByExcelFile.stream()
                .filter(str->!fileNameByDir
                        .contains(str))
                .collect(Collectors.toList()).forEach(System.out::println);

    }


    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Test
    public  void test2()
    {
        rabbitTemplate.convertAndSend("zhangpf","嘿嘿嘿");
    }

    @Test
    public void add()
    {
        System.out.println(2.0-1.1);
        BigDecimal subtract = BigDecimal.valueOf(2.0).subtract(BigDecimal.valueOf(1.1));
        System.out.println(subtract);
    }

    @Autowired
    private StringRedisTemplate redisTemplate;


    @Test
    public void reidsByString(){
        redisTemplate.opsForValue().set("one","222");
        //若存在存在k且v相同则不做操作,不相同则替换。
        Boolean one = redisTemplate.opsForValue().setIfPresent("two", "222");
        //若存在key则不做任何操作且返回false  不存在key则添加
        Boolean two = redisTemplate.opsForValue().setIfAbsent("two", "222");

        Map<String,String> stringMap=new HashMap<>();
        stringMap.put("one","111");
        stringMap.put("two","222");
        stringMap.put("three","333");
        stringMap.put("four","444");
        redisTemplate.opsForValue().multiSet(stringMap);
        List<String> strings = redisTemplate.opsForValue().multiGet(Arrays.stream(new String[]{"one", "two", "three"}).collect(Collectors.toList()));
        strings.stream().map((string)->{return string.toString();}).forEach(System.out::println);


        redisTemplate. opsForValue().set("hgy","heihei",15, TimeUnit.SECONDS);

        redisTemplate.expire("one",15,TimeUnit.SECONDS);

        Object one1 = redisTemplate.opsForValue().get("one");
        String s = redisTemplate.opsForValue().get("hgy");
        System.out.println(one1+"\n"+s);

    }



    @Test
    public void reidsByHash(){
        redisTemplate.opsForHash().put("en","黄国英","就这?");
        Map<String,String> stringMap=new HashMap<>();
        stringMap.put("我","?");
        stringMap.put("并","?");
        stringMap.put("没","?");
        stringMap.put("有","?");
        Boolean aBoolean = redisTemplate.opsForHash().putIfAbsent("en", "泊静", "就这");
        System.out.println(aBoolean);

    }

    @Test
    public void reidsBySet(){
        //往set结构中添加数据
        redisTemplate.opsForSet().add("我是谁","小黑","小白","小紫","内涵","强啊");
        redisTemplate.opsForSet().add("工作","zzz","ccc");
        //pop:弹出 弹出一个删除一个
        String who = redisTemplate.opsForSet().pop("我是谁");
        System.out.println(who);
        //remove:批量删除set中的某些值
        Long remove = redisTemplate.opsForSet().remove("我是谁", "小黑", "小白");
        System.out.println(remove);

        // randomMembers:获取set中的值并且不改变set中的值
        List<String> strings = redisTemplate.opsForSet().randomMembers("我是谁", 2L);
        strings.stream().forEach(System.out::println);
        System.out.println("----------------------");

        //unionc : 合并多个set的值
        redisTemplate.opsForSet().union("我是谁","工作").stream().forEach(System.out::println);

        //unionAndStore : 合并多个set后并且以最后一个参数做key存储到redis中
        redisTemplate.opsForSet().unionAndStore("我是谁", "工作", "test");

        //intersect获取两个set的交集   intersectAndStore:获取交集并保存到redis中
        redisTemplate.opsForSet().intersect("我是谁","test").stream().forEach(System.out::println);




    }

    @Test
    public void reidsByList(){
        //leftPush:往list中最左添加value
        redisTemplate.opsForList().leftPush("list", "left");
        //rightPush:往list中最右添加value
        redisTemplate.opsForList().rightPush("list", "right");
        //leftPop:往list中最左删除value
        String l = redisTemplate.opsForList().leftPop("list");
        //rightPop:往list中最右删除value
        String r = redisTemplate.opsForList().rightPop("list");

        //从key中获取l到l1的value ("list",0,-1)表示从list中获取所有value
        redisTemplate.opsForList().range("list",0,3).stream().forEach(System.out::println);

        // 从存储在键中的列表中删除等于值的元素的第一个计数事件。count> 0:删除等于从左到右移动的值的第一个元素;
        // count< 0:删除等于从右到左移动的值的第一个元素;
        // count = 0:删除等于value的所有元素。
        redisTemplate.opsForList().remove("list",3,"right");

        //  截取集合元素长度,保留长度内的数据
        redisTemplate.opsForList().trim("list",0,3);

        //在集合的指定位置插入元素,如果指定位置已有元素,则覆盖,没有则新增,超过集合下标+n则会报错。
        redisTemplate.opsForList().set("list",0,"zzz");

    }

    @Test
    public void redisZSet(){
        //add:添加元素到变量中同时指定元素的分值。
        redisTemplate.opsForZSet().add("zset","one",1);

        //range:获取变量指定区间的元素。
        redisTemplate.opsForZSet().range("zset",0,2);

        //rangeByLex:用于获取满足非score的排序取值。这个排序只有在有相同分数的情况下才能使用,如果有不同的分数则返回值不确定。
        redisTemplate.opsForZSet().rangeByLex("zset", RedisZSetCommands.Range.range(), RedisZSetCommands.Limit.limit());

        //rangeByScore:根据设置的score获取区间值。
        redisTemplate.opsForZSet().rangeByScore("zset",0,2);

        //remove:删除指定的value。
        redisTemplate.opsForZSet().remove("zset","one","two");

        // removeRange: 指定区间删除
        redisTemplate.opsForZSet().removeRange("zset",0,0);

        //removeRangeByScore:指定分数区间删除
        redisTemplate.opsForZSet().removeRangeByScore("zset",0,0);

        //score: 获取指定k v 的分数
        Double score = redisTemplate.opsForZSet().score("zset", "one");
        System.out.println(score);

        //score: 获取指定k1 和 k2的集合的并集 并添加到reids中用k3作为key存储
        redisTemplate.opsForZSet().unionAndStore("k1","k2","k3");

    }

}

标签:springboot,reids,list,System,API,println,import,redisTemplate,out
来源: https://blog.csdn.net/H_g_y_/article/details/112911237