承接国内外服务器租用托管、定制开发、网站代运营、网站seo优化托管接单、网站代更新,新老站点皆可!!咨询QQ:3787320601

字段:redis怎么做多字段排序

管理员 2023-03-29 08:24:44 互联网圈 41 ℃ 0 评论 3360字 收藏

redis怎样做多字段排序

redis如何做多字段排序

redis做多字段排序的示例:

在redis编辑器中添加以下代码:

package com.example.demo;

import com.example.demo.common.Constant;

import com.example.demo.mapper.QuestionAnswerHistoryMapper;

import com.example.demo.model.QuestionAnswerHistory;

import com.example.demo.service.QuestionAnswerHistoryService;

import com.example.demo.thread.BaseTest;

import org.apache.commons.lang3.RandomUtils;

import org.junit.Test;

import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;

import java.util.List;

/**

* @author wzx

* @time 2019/6/16

*/

public class QuestionAnswerHistoryTest extends BaseTest {

@Autowired

private QuestionAnswerHistoryMapper historyMapper;

@Autowired

private QuestionAnswerHistoryService historyService;

@Test

public void test() {

for (int i = 1; i < 101; i++) {

QuestionAnswerHistory history = new QuestionAnswerHistory();

history.setUserId((long) i);

history.setAnswerFinish(true);

BigDecimal bigDecimal = BigDecimal.valueOf(RandomUtils.nextDouble(0, 1))

.setScale(2, BigDecimal.ROUND_HALF_UP);

history.setCorrectRate(bigDecimal.doubleValue());

history.setTimeConsumed(RandomUtils.nextInt(15, 50));

history.setTimeConsumedMillisecond(RandomUtils.nextInt(0, 1000));

history.setStatus(Constant.GenerictStatus.NORMAL);

historyMapper.insert(history);

}

}

@Test

public void queryRanking() {

Listranking = historyService.getRanking();

for (QuestionAnswerHistory history : ranking) {

System.out.println(history.getUserId() + "\t" + history.getCorrectRate() + "\t" + history.getTimeConsumed() +

"\t" + history.getTimeConsumedMillisecond());

}

}

@Test

public void updateRank() {

Long userId = 25L;

//生成新的答题记录

QuestionAnswerHistory history = new QuestionAnswerHistory();

history.setUserId(userId);

history.setAnswerFinish(true);

BigDecimal bigDecimal = BigDecimal.valueOf(RandomUtils.nextDouble(0, 1))

.setScale(2, BigDecimal.ROUND_HALF_UP);

history.setCorrectRate(bigDecimal.doubleValue());

history.setTimeConsumed(RandomUtils.nextInt(15, 50));

history.setTimeConsumedMillisecond(RandomUtils.nextInt(0, 1000));

history.setStatus(Constant.GenerictStatus.NORMAL);

historyService.updateRank(history);

}

}

输入结果:

id 正确率 耗时

74 0.94 38 151

37 0.93 16 802

97 0.92 31 475

63 0.92 36 74

35 0.92 39 516

85 0.92 39 525

17 0.9 19 821

55 0.9 35 268

64 0.89 37 39

19 0.88 43 350

49 0.86 25 373

78 0.86 42 296

76 0.84 19 399

21 0.84 38 365

54 0.83 26 7

70 0.79 30 213

20 0.78 18 112

46 0.78 24 789

27 0.76 23 195

33 0.76 29 414

7 0.75 20 661

文章来源:丸子建站

文章标题:字段:redis怎么做多字段排序

https://www.wanzijz.com/view/5163.html

X

截屏,微信识别二维码

微信号:weimawl

(点击微信号复制,添加好友)

打开微信