首页 > 数据库 >Redis使用zset集合根据分值实现分页功能

Redis使用zset集合根据分值实现分页功能

时间:2023-01-06 18:24:21浏览次数:36  
标签:max zset int Redis 分值 result offset typedTuples

自己去看代码吧

@Component
public class QueryPage {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    private final String KEY="query:shop";

    /**
     * 向zset集合添加测试数据
     * */
    public void addData(){
        int score=0;
        int currentScore=0;
        for(int k=0;k<20;k++){
            currentScore=score;
            if(k%4==0){
                currentScore--;
            }
            stringRedisTemplate.opsForZSet().incrementScore(KEY,"shop:"+k,currentScore);
            score++;
            System.out.println("value:"+"shop:"+k+"  score:"+currentScore);
        }
    }

    /**
     * 查询小于等于max分值的数据,offset是偏移量
     * 第一次查询max可以赋予最大值999999999,offset赋值0
     * 后面翻页使用上次返回结果为max和offset赋值
     * */
    public Map<String,Object> query(long max,int offset){
        Map<String,Object> result=new HashMap<>();
        //查询分值小于等于max且最小值大于等于0的数据,查询3条
        //offset是偏移量,如数据中倒序的分值分别为5,5,4,4,3,3,2,,1,1
        //第一页查询offset=0结果为5,5,4
        //第二页查询offset=1结果为4,3,3 因为第一页中已经有4所以本次查询要把上次出现4的此次赋值给offset
        Set<ZSetOperations.TypedTuple<String>> typedTuples=stringRedisTemplate.opsForZSet().reverseRangeByScoreWithScores(KEY,0,max,offset,3);
        if(typedTuples == null || typedTuples.isEmpty()){
            // -1.表示没有数据
            return null;
        }
        //解析数据
        List<String> ids=new ArrayList<>(typedTuples.size());
        int minScore=0;
        int os=1;
        for(ZSetOperations.TypedTuple<String> tuple:typedTuples){
            //获取数据
            ids.add(tuple.getValue());
            //获取分值
            int score=tuple.getScore().intValue();
            if(score==minScore){
                os++;
            }else{
                minScore=score;
                os=1;
            }
        }
        result.put("offset",os);//用于后面翻页作为offset参数的值
        result.put("max",minScore);//用于后面翻页作为max参数的值
        result.put("data",ids);//查询到的数据
        return result;
    }
}

创建测试数据调用

addData()

翻页调用

//首页
Map<String,Object> result=query(99999,0);
//后面页使用上次返回的结果作为参数
int max=Integer.valueOf(result.get("max").toString()).intValue();
int offset=Integer.valueOf(result.get("offset").toString()).intValue();
Map<String,Object> other=query(max,offset);

标签:max,zset,int,Redis,分值,result,offset,typedTuples
From: https://www.cnblogs.com/big-strong-yu/p/17031282.html

相关文章

  • redis工具类
    操作Redis的工具类importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.data.redis.core.RedisTemplate;importorg.springfr......
  • docker安装redis教程
    安装docker参考博主另一篇文章 1、下载redisdockerpullredis//使用镜像加速下载redisdockerpullregistry.docker-cn.com/library/redis两种方法均可以2、下载完成后查......
  • Caused by: io.lettuce.core.RedisCommandExecutionException: WRONGPASS invalid use
    Redis连接报错RedisCommandExecutionException:WRONGPASSinvalidusername-passwordpair一、问题描述在application.yml中配置Redis连接信息如下:Redisredis:ho......
  • Linux-Windows-Mac-Redis安装教程
    说明本说明分一下三种方式安装,请根据具体环境选择相关版本。linux服务器安装win版服务器安装mac版安装linux服务器安装(本文档不含Redis集群的搭建,具体搭建方案请参考《​​......
  • Redis哨兵模式搭建
    一:哨兵主要作用监控:监控redis主库及从库运行状态;通知:如果redis发生故障转移,可以通过邮件通知管理员;自动故障转移:一旦发现主库宕机,则在从库中通过选举新的master进行故......
  • Redis实现秒杀功能 lua脚本判断库存、判断一人一单、添加到stream队列、异步处理订单
    需求:新增秒杀商品-将秒杀商品的id和秒杀数量添加到秒杀表中数据库操作将秒杀信息保存到Redis中基于Lua脚本,判断秒杀库存、一人一单,决定用户是否有下单资格如果抢购......
  • Windows下安装并设置Redis
    作者: ​​铁锚​​日期:2014年8月10日 ​​Redis​​对于​​Linux​​是官方支持的,安装和使用没有什么好说的,普通使用按照官方指导,5分钟以内就能搞定。详情请参考:​......
  • Redis缓存
    Redis缓存有哪些淘汰策略缓存被写满是不可避免的。即使你精挑细选,确定了缓存容量,还是要面对缓存写满时的替换操作。缓存替换需要解决两个问题:决定淘汰哪些数据,如何处理那......
  • Ubuntu 安装 Redis
    本文档记录使用Ubuntu安装一个生产可用的Redis实例。版本UbuntuUbuntu22.04.1LTSRedis7.0.7如果你正在运行一个非常小的发行版(比如Docker容器......
  • springboot使用redis实现计数限流
    lua脚本resources下创建文件redis/AccessLimit.lua内容为:locallimitSecond=tonumber(ARGV[1])locallimitMaxCount=tonumber(ARGV[2])localnum=tonumber(......