For more Spring articles, please click on the Grey Blog-Spring Feature
Among the five data structures of Redis, ZSET is the last one left at present, which can be simply understood as a set with weights. The biggest difference with the previous set is that each element can set a score, which can achieve various ranking functions
I. Basic use
To get started, specifying serialization requires additional processing, as mentioned in the List article:
- 181109-SpringBoot Advanced Redis List data structure using posture
1. Add elements
When adding a new element, it is used in the same way as a set, except that the score parameter is specified
If the element exists, it is replaced with a new score, returning 0; If the element does not exist, one will be added
/** * add an element to zset. The main difference between zset and set is that each element has a score. zadd * *@param key
* @param value
* @param score
*/
public void add(String key, String value, double score) {
redisTemplate.opsForZSet().add(key, value, score);
}
Copy the code
2. Delete elements
Delete is just like a normal set
/** * Delete element zrem **@param key
* @param value
*/
public void remove(String key, String value) {
redisTemplate.opsForZSet().remove(key, value);
}
Copy the code
3. Modify the score
After the element in zset is inserted, the score value can be modified, and the score can be added/subtracted by zincrby. When an element does not exist, a new one is inserted
From the above description, the biggest difference between Zincrby and ZAdd is that zincrby is incremental; The latter is the overlay score method
/** * score increases or decreases zincrby **@param key
* @param value
* @param score
*/
public Double incrScore(String key, String value, double score) {
return redisTemplate.opsForZSet().incrementScore(key, value, score);
}
Copy the code
4. Obtain score corresponding to value
The important thing to note here is that when a value is in the collection, its score is returned; If not, null is returned
/** * query value for score zscore **@param key
* @param value
* @return* /
public Double score(String key, String value) {
return redisTemplate.opsForZSet().score(key, value);
}
Copy the code
5. Obtain the ranking of value in the collection
Get the score corresponding to value; Here is getting the ranking; Here, the lower the score, the higher the ranking;
From this use, we can also see that combining 4 and 5, using Zset to make leaderboards can easily obtain the ranking and score of a user among all people
/** * determine the rank of value in zset@param key
* @param value
* @return* /
public Long rank(String key, String value) {
return redisTemplate.opsForZSet().rank(key, value);
}
Copy the code
6. Collection size
/** * returns the length of the collection **@param key
* @return* /
public Long size(String key) {
return redisTemplate.opsForZSet().zCard(key);
}
Copy the code
7. Get the data in the collection
Since it is ordered, it is possible to obtain a specified range of data in two ways
- Get data based on sort position
- Get the ranking position according to the score interval
Zrange ** returns the ordered set with the smallest score **@param key
* @param start
* @param end
* @return* /
public Set<String> range(String key, int start, int end) {
return redisTemplate.opsForZSet().range(key, start, end);
}
/** * select * from score (0, -1)@param key
* @param start
* @param end
* @return* /
public Set<ZSetOperations.TypedTuple<String>> rangeWithScore(String key, int start, int end) {
return redisTemplate.opsForZSet().rangeWithScores(key, start, end);
}
Zrevrange ** returns the ordered set with the highest score **@param key
* @param start
* @param end
* @return* /
public Set<String> revRange(String key, int start, int end) {
return redisTemplate.opsForZSet().reverseRange(key, start, end);
}
/** * get the set zrangebyScore ** according to the score value@param key
* @param min
* @param max
* @return* /
public Set<String> sortRange(String key, int min, int max) {
return redisTemplate.opsForZSet().rangeByScore(key, min, max);
}
Copy the code
II. The other
0. Project
- Engineering: the spring – the boot – demo
1. An ashy Blog
- A grey Blog Personal Blog blog.hhui.top
- A Grey Blog-Spring feature Blog Spring.hhui.top
A gray personal blog, recording all the study and work in the blog, welcome everyone to go to stroll
2. Statement
As far as the letter is not as good, the above content is purely one’s opinion, due to the limited personal ability, it is inevitable that there are omissions and mistakes, if you find bugs or have better suggestions, welcome criticism and correction, don’t hesitate to appreciate
- Micro Blog address: Small Gray Blog
- QQ: a gray /3302797840
3. Scan attention
A gray blog
Knowledge of the planet