侧边栏壁纸
博主头像
峰峰火火

一条咸鱼罢了

  • 累计撰写 121 篇文章
  • 累计创建 87 个标签
  • 累计收到 59 条评论

目 录CONTENT

文章目录

记一下Springboot + Redis五种类型操作

峰峰火火
2022-11-25 / 0 评论 / 0 点赞 / 380 阅读 / 498 字 / 正在检测是否收录...
温馨提示:
若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。
 /**
     * list
     */
    @Test
    public void list() {
        redisTemplate.opsForList().leftPush("list1", "1");
        redisTemplate.opsForList().leftPush("list1", "2");
        redisTemplate.opsForList().leftPush("list1", "3");

        System.out.println(redisTemplate.opsForList().rightPop("list1"));
        System.out.println(redisTemplate.opsForList().rightPop("list1"));
        System.out.println(redisTemplate.opsForList().rightPop("list1"));


        List<Object> range = redisTemplate.opsForList().range("list1", 0, redisTemplate.opsForList().size("list1"));
        assert range != null;
        System.out.println(Arrays.toString(range.toArray()));
    }
    /**
     * hashmap
     */
    @Test
    public void hash() {
        redisTemplate.opsForHash().put("hash1", "key1", "value1");
        redisTemplate.opsForHash().put("hash1", "key2", "value2");
        redisTemplate.opsForHash().put("hash1", "key3", "value3");

        redisTemplate.opsForHash().put("hash1", "key1", "value4");


        System.out.println(redisTemplate.opsForHash().get("hash1", "key1"));
        System.out.println(redisTemplate.opsForHash().get("hash1", "key2"));
        System.out.println(redisTemplate.opsForHash().get("hash1", "key3"));
    }
    /**
     * set
     */
    @Test
    public void set() {
        redisTemplate.opsForSet().add("set1", 1,2,3,4,5,6);

        System.out.println(redisTemplate.opsForSet().pop("set1"));
        System.out.println(redisTemplate.opsForSet().pop("set1"));
        System.out.println(redisTemplate.opsForSet().pop("set1"));

        redisTemplate.opsForSet().move("set1",1,"set2");
    }

    /**
     * 根据权重排序
     */
    @Test
    public void z_set() {
        redisTemplate.opsForZSet().add("z-set", "z-set1", 1);
        redisTemplate.opsForZSet().add("z-set", "z-set2", 2);
        redisTemplate.opsForZSet().add("z-set", "z-set3", 3);


        Cursor<ZSetOperations.TypedTuple<Object>> cursor = redisTemplate.opsForZSet().scan("z-set", ScanOptions.NONE);
        while (cursor.hasNext()) {
            ZSetOperations.TypedTuple<Object> next = cursor.next();
            System.out.println(next.getValue() + ":" + next.getScore());
        }
    }

    /**
     * 保存经纬度
     */
    @Test
    public void geo() {
        redisTemplate.opsForGeo().add("geo1", new Point(1,2), "北京");
        redisTemplate.opsForGeo().add("geo1", new Point(80,80), "上海");
        redisTemplate.opsForGeo().add("geo1", new Point(60,70), "深圳");

        List<Point> position = redisTemplate.opsForGeo().position("geo1", "北京", "上海", "深圳");

        if (position != null) {
            for (Point point : position) {
                System.out.println(point.toString());;
            }
        }

        System.out.println(redisTemplate.opsForGeo().distance("geo1", "北京", "上海").getMetric().getMultiplier());

        redisTemplate.opsForGeo().remove("geo1", "北京");

    }

    /**
     * 适用于 点赞功能
     */
    @Test
    public void hyperLogLog() {
        redisTemplate.opsForHyperLogLog().add("HyperLogLog", "a", "b" , "c", "d", "a");
        redisTemplate.opsForHyperLogLog().add("HyperLogLog2", "a");
        redisTemplate.opsForHyperLogLog().add("HyperLogLog2", "z");

        System.out.println(redisTemplate.opsForHyperLogLog().size("HyperLogLog"));
        System.out.println(redisTemplate.opsForHyperLogLog().size("HyperLogLog2"));
    }
0

评论区