Preface
Based on redis to implement the ranking list
What we need to do now is to rank the companies. The ranking standard is the number of searches for the company by users, and make a ranking of the top ten companies
1 .Related redis knowledge
The redis data structure related to the implementation of the ranking function is sort set (ordered set)
About sort set
We know that set is a Sets, one of the characteristics of sets is that they have no duplicate elements. In addition to having no duplicate elements, sort sets also have the characteristic of orderliness.
Data structure composition: Weight: also called score (score) redis sorts the elements in the set in ascending order by weight Sorting (default), weight can be repeated ######value: set elements, elements cannot be repeated ######String(set key),double(权重),String(value)###sort set is implemented through a hash table, so adding, function, and search time The complexity is O(1), and each collection can store more than 4 billion elements######Basic commands#########Add one or more elements to the collection##### #
ZADD "KEY" SCORE "VALUE" [ SCORE "VALUE"]###Effect:###
MyRedis:0>ZADD test 1 "one""1"MyRedis:0>zadd test 4 "four" 5 "five""2"######Get the number of elements in the collection######
ZCARD "key"###Effect###
MyRedis:0>ZCARD test"5"######Get the specified element score (weight) ######
ZSCORE "KEY" "VALUE"###Effect###
MyRedis:0>ZSCORE "test" "one""2"######The specified element of the specified collection increases the specified score######
ZINCRBY "key" score "value"###Effect: ###
MyRedis:0>ZSCORE "test" "one""2"MyRedis:0>ZINCRBY "test" 1 "one""3"MyRedis:0>ZSCORE "test" "one" "3"##### #Get the elements in the specified range (the default is in ascending order of score|weight)######
ZRANGE "key" 开始下标 结束下标###Effect###
MyRedis:0>ZRANGE "test" 0 1 1) "two" 2) "one"###It takes about so many commands to complete this requirement, let’s start implementing ours Requirements######2.springboot redis implementation######Import redis dependency###
<dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-data-redis</artifactid> </dependency>###Write tool class###
//=============================== sort set ================================= /** * 添加指定元素到有序集合中 * @param key * @param score * @param value * @return */ public boolean sortSetAdd(String key,double score,String value){ try{ return redisTemplate.opsForZSet().add(key,value,score); }catch (Exception e){ e.printStackTrace(); return false; } } /** * 有序集合中对指定成员的分数加上增量 increment * @param key * @param value * @param i * @return */ public double sortSetZincrby(String key,String value,double i){ try { //返回新增元素后的分数 return redisTemplate.opsForZSet().incrementScore(key, value, i); }catch(Exception e){ e.printStackTrace(); return -1; } } /** * 获得有序集合指定范围元素 (从大到小) * @param key * @param start * @param end * @return */ public Set sortSetRange(String key,int start,int end){ try { return redisTemplate.opsForZSet().reverseRange(key, start, end); }catch (Exception e){ e.printStackTrace(); return null; } }###Business implementation:######### ######Because the ranking has high real-time requirements, I personally think it is not necessary to persist it to the database###
/** * 根据公司名找到指定公司 * @param companyName * @return */ @Override public AjaxResult selectCompanyName(String companyName) { Set<object> set = redisUtils.sGet("company"); for(Object i : set){ String json = JSONObject.toJSONString(i); JSONObject jsonObject = JSONObject.parseObject(json); if(jsonObject.getString("companyName").equals(companyName)){ //搜索次数 + 1 redisUtils.sortSetZincrby("companyRank",companyName,1); log.info("直接缓存中返回"); return new AjaxResult().ok(jsonObject); } } log.error("缓存中没有,查数据库"); TbCommpanyExample tbCommpanyExample = new TbCommpanyExample(); tbCommpanyExample.createCriteria().andCompanyNameEqualTo(companyName); List<tbcommpany> list = tbCommpanyMapper.selectByExample(tbCommpanyExample); if(list.size() != 0){ //放入缓存中 redisUtils.sSet("company",list.get(0)); //数据库中存在 //搜索次数 + 1 redisUtils.sortSetZincrby("companyRank",companyName,1); log.info("sql"); return new AjaxResult().ok(list.get(0)); }else{ return new AjaxResult().error("没有找到该公司:"+companyName); } }</tbcommpany></object>###Get the ranking###
/** * 获得公司排行榜(前十) * @return */ @Override public AjaxResult getCompanyRank() { Set set = redisUtils.sortSetRange("companyRank",0,9); if(set.size() == 0){ return new AjaxResult().error("公司排行榜为空"); } return new AjaxResult().ok(set); }###3. Testing and summary#### ###########postman test: ###############Another question is the ranking of the same scores######If I want A to be The one who arrived first is ranked in front of B who has the same score but arrived later. How to solve this problem? ######To solve this problem, we can consider adding a timestamp to the score. The calculation formula is: ###
带时间戳的分数 = 实际分数*10000000000 + (9999999999 – timestamp)###This company with time can write it by itself to reduce the error as much as possible###### ###############
The above is the detailed content of About java implementing ranking list based on redis ordered collection. For more information, please follow other related articles on the PHP Chinese website!

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于结构化数据处理开源库SPL的相关问题,下面就一起来看一下java下理想的结构化数据处理类库,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于PriorityQueue优先级队列的相关知识,Java集合框架中提供了PriorityQueue和PriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlockingQueue是线程安全的,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于java锁的相关问题,包括了独占锁、悲观锁、乐观锁、共享锁等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于枚举的相关问题,包括了枚举的基本操作、集合类对枚举的支持等等内容,下面一起来看一下,希望对大家有帮助。

封装是一种信息隐藏技术,是指一种将抽象性函式接口的实现细节部分包装、隐藏起来的方法;封装可以被认为是一个保护屏障,防止指定类的代码和数据被外部类定义的代码随机访问。封装可以通过关键字private,protected和public实现。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于设计模式的相关问题,主要将装饰器模式的相关内容,指在不改变现有对象结构的情况下,动态地给该对象增加一些职责的模式,希望对大家有帮助。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SublimeText3 Linux new version
SublimeText3 Linux latest version

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 English version
Recommended: Win version, supports code prompts!
