文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

redis怎么实现热点排序

2024-04-02 19:55

关注

redis怎么实现热点排序

redis实现热点排序的示例:

使用springboot+redis实现热词搜索,代码如下:

import cc.datebook.common.JsonResult;

import cc.datebook.entity.BookInfo;

import cc.datebook.enums.ResultCode;

import cc.datebook.service.BookInfoService;

import org.apache.commons.lang3.StringUtils;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

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

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

import org.springframework.data.redis.core.RedisTemplate;

import org.springframework.data.redis.core.ValueOperations;

import org.springframework.data.redis.core.ZSetOperations;

import org.springframework.web.bind.annotation.RequestBody;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

import java.util.*;

@RestController

@RequestMapping("/hotKey")

public class HotKeyController {

private Logger logger = LoggerFactory.getLogger(this.getClass());

@Resource(name = "redisKeyDatabase")

private RedisTemplate redisKeyDatabase;

@Resource(name = "redisKeyTimeDatabase")

private RedisTemplate redisKeyTimeDatabase;

@Autowired

private BookInfoService bookInfoService;

@RequestMapping(value = "/add", method = RequestMethod.POST)

public JsonResult redisAdd() {

List bookInfos = bookInfoService.getBookInfos();

for (BookInfo bookInfo : bookInfos) {

if (StringUtils.isNotBlank(bookInfo.getAuthor())) {

String author = bookInfo.getAuthor().replaceAll("\\﹝.*?\\﹞|\\〔.*?\\〕|\\(.*?\\)|\\{.*?}|\\[.*?]|\\[.*?]|\\【.*?】|(.*?)|等|著|编者|主编|[^0-9a-zA-Z\u4e00-\u9fa5.,,•·《 》]", "").trim();

author = author.replaceAll("\\s+", " ").trim();

try {

if (author.endsWith(",") || author.endsWith(",")) {

author = author.substring(0, author.length() - 1);

}

} catch (Exception e) {

logger.error("1111111" + bookInfo.toString());

}

try {

String tem = author.substring(author.lastIndexOf(",") + 1, author.length());

String tem1 = author.substring(author.lastIndexOf(",") + 1, author.length());

if (StringUtils.isBlank(tem)) {

author = author.substring(0, author.lastIndexOf(","));

}

if (StringUtils.isBlank(tem1)) {

author = author.substring(0, author.lastIndexOf(","));

}

} catch (Exception e) {

logger.error("2222222" + bookInfo.toString());

}

bookInfo.setAuthor(author);

bookInfoService.updateByPrimaryKeySelective(bookInfo);

}

}

Long now = System.currentTimeMillis();

ZSetOperations zSetOperations = redisKeyDatabase.opsForZSet();

ValueOperations valueOperations = redisKeyTimeDatabase.opsForValue();

List title = bookInfoService.getBookTitle();

List author = bookInfoService.getBookAuthor();

for (int i = 0, lengh = title.size(); i < lengh; i++) {

String tle = title.get(i);

try {

if (zSetOperations.score("title", tle) <= 0) {

zSetOperations.add("title", tle, 0);

valueOperations.set(tle, now);

}

} catch (Exception e) {

zSetOperations.add("title", tle, 0);

valueOperations.set(tle, now);

}

}

for (int i = 0, lengh = author.size(); i < lengh; i++) {

String aut = author.get(i);

if (StringUtils.isNotBlank(aut)) {

String auth[] = aut.split(",|,");

for (String str : auth) {

if (StringUtils.isNotBlank(str)) {

try {

if (zSetOperations.score("title", str.trim()) <= 0) {

zSetOperations.add("title", str.trim(), 0);

valueOperations.set(str.trim(), now);

}

} catch (Exception e) {

zSetOperations.add("title", str.trim(), 0);

valueOperations.set(str.trim(), now);

}

}

}

}

}

return new JsonResult(ResultCode.SUCCESS, "成功");

}

@RequestMapping(value = "/get", method = RequestMethod.POST)

public JsonResult redisGet(@RequestBody Map params) {

String key = params.get("key").toString();

Long now = System.currentTimeMillis();

List result = new ArrayList<>();

ZSetOperations zSetOperations = redisKeyDatabase.opsForZSet();

ValueOperations valueOperations = redisKeyTimeDatabase.opsForValue();

Set value = zSetOperations.reverseRangeByScore("title", 0, Double.MAX_VALUE);

//key不为空的时候 推荐相关的最热前十名

for (String val : value) {

if (StringUtils.containsIgnoreCase(val, key)) {

if (result.size() > 9) {//只返回最热的前十名

break;

}

Long time = valueOperations.get(val);

if ((now - time) < 2592000000L) {//返回最近一个月的数据

result.add(val);

} else {//时间超过一个月没搜索就把这个词热度归0

zSetOperations.add("title", val, 0);

}

}

}

return new JsonResult(ResultCode.SUCCESS, "成功", result);

}

@RequestMapping(value = "/incrementScore", method = RequestMethod.POST)

public JsonResult incrementScore(@RequestBody Map params) {

String key = params.get("key").toString();

Long now = System.currentTimeMillis();

ZSetOperations zSetOperations = redisKeyDatabase.opsForZSet();

ValueOperations valueOperations = redisKeyTimeDatabase.opsForValue();

zSetOperations.incrementScore("title", key, 1);

valueOperations.getAndSet(key, now);

return new JsonResult(ResultCode.SUCCESS, "成功");

}

}

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     807人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     351人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     314人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     433人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     221人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯