栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot整合redis

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot整合redis

1. 采用jedis 引入相关依赖
		
            org.springframework.boot
            spring-boot-starter-data-redis
            
                
                    io.lettuce
                    lettuce-core
                
            
        

        
            redis.clients
            jedis
        
2. 常见的相关操作
package com.zzh;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.DefaultTypedTuple;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

@SpringBootTest
class RedisDemoApplicationTests {
    @Resource
    private StringRedisTemplate template;

    @Test
    void contextLoads() {
    }

    @Test
    public void testOne() {
        template.opsForValue().set("name", "root");
        String name = template.opsForValue().get("name");
        System.out.println(name);
    }
	
	// 对zset相关操作
    @Test
    public void testTwo() {
        Set> tuples = new HashSet<>();

        for (int i = 0; i <10000; i++) {

            DefaultTypedTuple tuple = new DefaultTypedTuple<>("张" + i, 1D + i);
            tuples.add(tuple);
        }

        Long score_rank = template.opsForZSet().add("score_rank", tuples);
        System.out.println(score_rank);
    }
	
	// 获取分数
    @Test
    public void testGetRank() {

        Long rank  = template.opsForZSet().reverseRank("score_rank", "张9999");
        assert rank != null;
        System.out.println(rank + 1L);

    }
	
	// 获取分数的前10名
    @Test
    public void testGetTop10() {

        Set top10Set = template.opsForZSet().reverseRange("score_rank", 0, 10);
        System.out.println(top10Set);

        Set> tuples = template.opsForZSet()
        .reverseRangeWithScores("score_rank", 0, 10);

        assert tuples != null;
        List list = tuples.stream()
        .filter(t -> t.getScore() != null && t.getValue() != null)
        .map(t -> new ScoreRankDto(t.getValue(), t.getScore().longValue()))
        .collect(Collectors.toList());
        System.out.println(list);

    }

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/937347.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号