zset type, structure and application scenario of Redis

Keywords: Redis

Zset type and structure


The redis ordered set is also a part of the set type, so it retains the feature that the elements in the set cannot be repeated. However, the difference is that the ordered set sets an additional score for each element, which is used as the basis for sorting.

Ordered sets can be sorted from small to large using scores. Although the members of an ordered set are unique, scores can be repeated. For example, in a class, the student number is unique, but the scores of each subject can be the same. redis can use an orderly collection to store student scores and quickly do the score ranking function.


Common commands

zadd command

Description: adds one or more elements and their fractions to an ordered set. If the added element already exists, update the score value of the member, and then re insert it to locate its position. If the key does not exist, create a new ordered collection and insert it.

Command use: [zadd command] [key name] [value field] [value value]

127.0.0.1:6379> zadd score zhangsan 80 xiaoming 90 lisi 100
(error) ERR value is not a valid float
127.0.0.1:6379> zadd score 80 zhangsan 90 xiaoming 100 lisi
(integer) 3
127.0.0.1:6379> 

zcard command

Description: used to calculate the number of elements in an ordered set.

Command use: [zcard command] [key name]

127.0.0.1:6379> zcard score
(integer) 3
127.0.0.1:6379> 

zcount command

Description: used to calculate the number of members of a specified score interval in an ordered set.

The command uses: [zcount command] [key name] [field start value] [field end value]

127.0.0.1:6379> zcount score 80 90
(integer) 2
127.0.0.1:6379> 

zrange command

Description: used to return members within a specified interval. The positions of members are sorted by increasing points (from small to large). The subscript parameters here all start from 0. A negative number means from the last member, and - 1 means the last member.

Command use: [zrange command] [key name] [start subscript] [end subscript]

127.0.0.1:6379> zrange score 0 -1
1) "zhangsan"
2) "xiaoming"
3) "lisi"
127.0.0.1:6379> 

Zrevereange command

Description: used to return members within the specified score range. The positions of members are sorted by decreasing fractional value (from large to small).

Command use: [zrevereange command] [key name] [start subscript] [end subscript]

127.0.0.1:6379> ZREVRANGE score 0 -1
1) "lisi"
2) "xiaoming"
3) "zhangsan"
127.0.0.1:6379> 

zrangebyscore command

Description: returns the member list of the specified score interval in the ordered set. The integrators are sorted by increasing score (from small to large).

Command use: [zrangebyscore command] [key name] [start value] [end value]

127.0.0.1:6379> zrangebyscore score 80 90
1) "zhangsan"
2) "xiaoming"
127.0.0.1:6379>

zrank command

Description: used to return the ranking of specified members in an ordered set.

Command use: [zrank command] [key name] [value]

127.0.0.1:6379> 
127.0.0.1:6379> zrank score xiaoming
(integer) 1
127.0.0.1:6379> zrank score zhangsan
(integer) 0
127.0.0.1:6379> zrank score lisi
(integer) 2
127.0.0.1:6379> 

zrem command

Description: used to remove one or more members from an ordered set. Nonexistent members will be ignored.

Command use: [zrem command] [key name] [value value]

127.0.0.1:6379> zrem score xiaoming
(integer) 1
127.0.0.1:6379> zrange score 0 -1
1) "zhangsan"
2) "lisi"
127.0.0.1:6379> 

zremrangebyrank command

Description: used to remove all members in the specified rank interval from the ordered set.

Command use: [zremrangebyrank command] [key name] [start subscript] [end subscript]

127.0.0.1:6379> zremrangebyrank score 0 0
(integer) 1
127.0.0.1:6379> zrange score 0 -1
1) "lisi"
127.0.0.1:6379> 

zscore command

Description: returns the score value of members in an ordered set. If the member element is not a member of the ordered set key, or the key does not exist, nil is returned.

Command use: [zscore command] [key name] [value value]

127.0.0.1:6379> zadd score 66 xiaohe
(integer) 1
127.0.0.1:6379> zscore score xiaohe
"66"
127.0.0.1:6379> 

zscan command

Description: used to iterate over elements in an ordered set (including element members and element scores).

Command use: [zscan command] [key name] [cursor value] [matching pattern] [find content x * represents content beginning with x]

127.0.0.1:6379> zscan score 0 match x*
1) "0"
2) 1) "xiaohe"
   2) "66"
127.0.0.1:6379>



Zset type application scenario


1, Ranking list

Orderly collection of classic usage scenarios. For example, a video website needs to make a ranking list of videos uploaded by users. The maintenance of the list may be in many aspects: according to time, according to playback volume, according to the number of likes obtained, etc.

The following is a simple implementation code (for reference only):

<?php
	public function show(Product $product,Request $request)
	{
	
		if (!$product->status){
		
			throw new \Exception("The product is not on the shelf");
			
		}
		
		if (!Redis::exists("product:".$product->id.":count")){
		
			Redis::set("product:".$product->id.":count",1);
			
		}else{
		
			Redis::incr("product:".$product->id.":count",1);
			
		}
		
		Redis::zadd("product:Ranking",Redis::get("product:".$product->id.":count"),$product->id);
		
		return view("product.productShow",["product" => $product]);
		
	}
	
?>

In order to better highlight the effect, redis uses string to record every time a commodity is browsed. Meanwhile, zset type will modify the scores of related commodities according to the records of string to ensure the real-time update of commodity ranking.

When users view leaderboards:

127.0.0.1:6379> ZREVRANGE product:Ranking 0 -1
1) "2"
2) "1"




Posted by rameshmrgn on Tue, 07 Sep 2021 16:38:24 -0700