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 String tle = title.get(i);
try {
if (zSetOperations.score("title", tle)
版权声明
本文仅代表作者观点,不代表米安网络立场。
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。