Redis five data types (string object (string), list object (list), hash object (hash), unordered set (set) and Sorted Set data type (zset ordered set))

1. String data type

  • Overview:

  • String is the most basic type of redis. You can understand it as exactly the same type as Memcached. A key corresponds to a value.

  • The String type is binary safe. This means that the redis string can contain any data. Such as jpg images or serialized objects.

  • The String type is the most basic data type of Redis, and a key can store up to 512MB.

1.SET/GET/APPEND/STRLEN

command Explanation
SET Add value
GET View value
APPEND If the key does not exist, return the length of the current Value, if the key exists, return the length of Value after append
STRLEN Get key length
redis 127.0.0.1:6379> exists word						   #判断该键是否存在,存在返回1,否则返回0。
(integer) 0		
redis 127.0.0.1:6379> append word "new"				       #该键并不存在,因此append命令返回当前Value的长度。
(integer) 3		
redis 127.0.0.1:6379> append word " life"		  		   #该键已经存在,因此返回追加后Value的长度。
(integer) 8
redis 127.0.0.1:6379> get word						       #通过get命令获取该键,以判断append的结果。
"new life"	
redis 127.0.0.1:6379> set word "dont complain feel life"   #通过set命令为键设置新值,并覆盖原有值。
OK
redis 127.0.0.1:6379> get word                             #查看word键的值
"dont complain feel life"
redis 127.0.0.1:6379> strlen word					       #获取指定Key的字符长度。
(integer) 23

Insert picture description here

2.INCR/DECR/INCRBY/DECRBY

command Explanation
INCR Increase
DECR Decrease
INCRBY Increase the specified value
DECRBY Decrease the specified value
redis 127.0.0.1:6379> set word 10						#设置Key的值为10
OK
redis 127.0.0.1:6379> incr word					   	    #该Key的值递增1
(integer) 11
redis 127.0.0.1:6379> decr word					        #该Key的值递减1
(integer) 10
redis 127.0.0.1:6379> del word							#删除已有键。
(integer) 1
redis 127.0.0.1:6379> decr word							#对空值执行递减操作,其原值被设定为0,递减后的值为-1
(integer) -1
redis 127.0.0.1:6379> del word	                        #删除已有键。
(integer) 1
redis 127.0.0.1:6379> incr word							#对空值执行递增操作,其原值被设定为0,递增后的值为1
(integer) 1
redis 127.0.0.1:6379> set word	 hallo					#将该键的Value设置为不能转换为整型的普通字符串。
OK
redis 127.0.0.1:6379> incr word	                        #incr只能对数字执行,否则会报错
(error) ERR value is not an integer or out of range
redis 127.0.0.1:6379> set word	 6
OK
redis 127.0.0.1:6379> decrby word 3			 	        #减少指定的整数
(integer) 3
redis 127.0.0.1:6379> incrby word	 9					#增加指定的整数
(integer) 12

Insert picture description here

3.GETSET

command Explanation
GETSET Get the original value and set the new value
redis 127.0.0.1:6379> incr myword					#将计数器的值原子性的递增1
(integer) 1

redis 127.0.0.1:6379> getset myword 3	            #在获取计数器原有值的同时,并将其设置为新值,这两个操作原子性的同时完成。
"1"
redis 127.0.0.1:6379> get myword						#查看设置后的结果。
"3"

Insert picture description here

4. SETEX

command Explanation
SETEX Set time to live
redis 127.0.0.1:6379> setex myword 10 "hallo"			#设置指定Key的过期时间为10秒。
OK    
redis 127.0.0.1:6379> ttl myword			                #通过ttl命令查看一下指定Key的剩余存活时间(秒数),-2表示已经过期,-1表示永不过期。
(integer) 15
redis 127.0.0.1:6379> get myword							#在该键的存活期内我们仍然可以获取到它的Value。
"hello"
redis 127.0.0.1:6379> ttl myword							#该ttl命令的返回值显示,该Key已经过期。
(integer) 0
redis 127.0.0.1:6379> get myword							#获取已过期的Key将返回nil。
(nil)

Insert picture description here

5.SETNX

command Explanation
SETNX The command is executed successfully when the key does not exist, and the command fails when the key exists
redis 127.0.0.1:6379> del myword							#删除该键,以便于下面的测试验证。
(integer) 1
redis 127.0.0.1:6379> setnx myword "new"				#该键并不存在,因此setnx命令执行成功。
(integer) 1
redis 127.0.0.1:6379> setnx myword "life"				#该键已经存在,因此本次设置没有产生任何效果。
(integer) 0
redis 127.0.0.1:6379> get myword							#从结果可以看出,返回的值仍为第一次设置的值。
"new"

Insert picture description here

6.MSET/MGET/MSETNX

command Explanation
MSET Add keys in bulk
MGET Get key values ​​in batch
MSETNX Add keys in batches. If the added key does not exist, the execution will succeed. If the key to be added already exists, the execution will fail.
redis 127.0.0.1:6379> mset ke1 "hello" ke2 "world"	#批量设置了ke1和ke2两个键。
OK
redis 127.0.0.1:6379> mget ke1 ke2					#批量获取了ke1和ke2两个键的值。
1) "hello"
2) "world"
redis 127.0.0.1:6379> msetnx ke3 "new" ke4 "life" 	#批量设置了ke3和ke4两个键,因为之前他们并不存在,所以msetnx命令执行成功并返回1。
(integer) 1
redis 127.0.0.1:6379> mget ke3 ke4
1) "new"
2) "life"
redis 127.0.0.1:6379> msetnx ke3 "hello" ke5 "world"	#批量设置了ke3和ke5两个键,但是ke3已经存在,所以msetnx命令执行失败并返回0。
(integer) 0
redis 127.0.0.1:6379> mget ke3 ke5					#批量获取ke3和ke5,由于ke5没有设置成功,所以返回nil。
1) "new"
2) (nil)

Insert picture description here

Two. List (list) data type

  • Overview: The element type of the list is string, sorted in the order of insertion, and elements are added to the head or tail of the list

1. LPUSH/LPUSHX/LRANGE

command Explanation
LPUSH When the key does not exist, the key and its associated List will be created, and the values ​​in the parameter will be inserted from left to right.
LPUSHX When the key does not exist, no operation will be performed and the return value is 0
LRANGE View the elements in the linked list
redis 127.0.0.1:6379> del myword
(integer) 1
redis 127.0.0.1:6379> lpush myword 1 2 3 4   	#myword键并不存在,该命令会创建该键及与其关联的List,之后在将参数中的values从左到右依次插入。
(integer) 4
redis 127.0.0.1:6379> lrange myword  0 -1		#取链表中的全部元素,其中0表示第一个元素,-1表示最后一个元素。
1) "4"
2) "3"
3) "2"
4) "1"
redis 127.0.0.1:6379> lrange myword 0 1		    #取从位置0开始到位置1结束的2个元素。
1) "4"
2) "3"
3) "2"
redis 127.0.0.1:6379> lpushx myword2 e		    #myword2键此时并不存在,因此lpushx命令将不会进行任何操作,其返回值为0。
(integer) 0
redis 127.0.0.1:6379> lrange myword2 0 -1	    #可以看到myword2没有关联任何List Value。
(empty list or set)
redis 127.0.0.1:6379> lpushx myword e		    #myword键此时已经存在,所以lpushx命令插入成功,并返回链表中当前元素的数量。
(integer) 5
redis 127.0.0.1:6379> lrange myword 0 0		    #获取该键的List Value的头部元素。
1) "e"

Insert picture description here

2. LPOP / FULL

command Explanation
LPOP Remove and return the first element of the key, go from the left
FULL View the number of elements in the linked list
redis 127.0.0.1:6379> del myword
(integer) 1
redis 127.0.0.1:6379> lpush myword 1 2 3 4  #当myword键不存在时,会创建该键与其关联的List,参数中的values从左到
(integer) 4
redis 127.0.0.1:6379> lpop myword			#移除并返回myword键的第一个元素,从左取
"4"
redis 127.0.0.1:6379> lpop myword
"3"
redis 127.0.0.1:6379> llen myword			#在执行lpop命令两次后,链表头部的两个元素已经被弹出,此时链表中元素的数量是2
(integer) 2

Insert picture description here

3. LREM / LSET / LINDEX / LTRIM

command Explanation
LREM From the head (left) to the tail (right) of the variable list, delete the specified number of elements with the specified value, and the return value is the actual deleted amount
LSET Change the element value contained in the index value
LINDEX Get the element value contained in the index value, the head is 0 and the tail is increased
LTRIM The value of the element contained in the custom quantity index
redis 127.0.0.1:6379> del myword
(integer) 1
redis 127.0.0.1:6379> lpush myword 1 2 3 4 1 3		#为后面的示例准备测试数据。
(integer) 6
redis 127.0.0.1:6379> lrem myword 2 1		#从头部(left)向尾部(right)变量链表,删除2个值等于1的元素,返回值为实际删除的数量。
(integer) 2
redis 127.0.0.1:6379> lrange mykey 0 -1		#看出删除后链表中的全部元素。
1) "3"
2) "4"
3) "3"
4) "2"
redis 127.0.0.1:6379> lindex myword 1		#获取索引值为1(头部的第二个元素)的元素值。
"d"
redis 127.0.0.1:6379> lset myword 1 x		#将索引值为1(头部的第二个元素)的元素值设置为新值x。
OK
redis 127.0.0.1:6379> lindex myword 1		#查看是否设置成功。
"x"
redis 127.0.0.1:6379> lindex myword 6		#索引值6超过了链表中元素的数量,该命令返回nil。
(nil)
redis 127.0.0.1:6379> lset myword 6 hh		#设置的索引值6超过了链表中元素的数量,设置失败,该命令返回错误信息。
(error) ERR index out of range
redis 127.0.0.1:6379> ltrim myword 0 2		#仅保留索引值0到2之间的3个元素,注意第0个和第2个元素均被保留。
OK
redis 127.0.0.1:6379> lrange myword 0 -1		#查看trim后的结果。
1) "3"
2) "x"
3) "3" 

Insert picture description here

4. LINSERT

command Explanation
LINSERT Insert a new element before and after the element and use it with (before: before) (after: after)
redis 127.0.0.1:6379> del mykey						#删除该键便于后面的测试。
(integer) 1
redis 127.0.0.1:6379> lpush mykey a b c d e			#为后面的示例准备测试数据。
(integer) 5
redis 127.0.0.1:6379> linsert mykey before a a1		#在a的前面插入新元素a1。
(integer) 6。
redis 127.0.0.1:6379> lrange mykey 0 -1				#查看是否插入成功,从结果看已经插入
1) "e"
2) "d"
3) "c"
4) "b"
5) "a1"
6) "a"
redis 127.0.0.1:6379> linsert mykey after e e2		#在e的后面插入新元素e2,从返回结果看已经插入成功。
(integer) 7
redis 127.0.0.1:6379> lindex mykey 1				#再次查看是否插入成功。
"e2"
redis 127.0.0.1:6379> linsert mykey after k a		#在不存在的元素之前或之后插入新元素,linsert命令操作失败,并返回-1。
(integer) -1
redis 127.0.0.1:6379> linsert mykey1 after a a2		#为不存在的Key插入新元素,linsert命令操作失败,返回0。
(integer) 0

Insert picture description here

5. RPUSH/RPUSHX/RPOP/RPOPLPUSH

command Explanation
RPUSH Insert elements from the end of the linked list, the insertion order is from left to right (positive order)
RPUSHX In the key of an existing element, insert a new element from the end
RPOP Remove and return the first element in the key, taken from the right (taken from the tail)
RPOPLPUSH Pop (move) the tail element of the original key and insert it into the head of the new key
redis 127.0.0.1:6379> del mykey						#删除该键,以便于后面的测试。
(integer) 1
redis 127.0.0.1:6379> rpush mykey a b c d			#从链表的尾部插入参数中给出的values,插入顺序是从左到右依次插入。
(integer) 4
redis 127.0.0.1:6379> lrange mykey 0 -1				#通过lrange命令可以获悉rpush在插入多值时的插入顺序。
1) "a"
2) "b"
3) "c"
4) "d"
redis 127.0.0.1:6379> rpushx mykey e	            #该键已经存在并且包含4个元素,rpushx命令将执行成功,并将元素e插入到链表的尾部。
(integer) 5
redis 127.0.0.1:6379> lindex mykey 4	            #通过lindex命令可以看出之前的rpushx命令确实执行成功,因为索引值为4的元素已经是新元素了。
"e"
redis 127.0.0.1:6379> rpushx mykey2 e		        #由于mykey2键并不存在,因此rpushx命令不会插入数据,其返回值为0。
(integer) 0
redis 127.0.0.1:6379> lrange mykey 0 -1		        #在执行rpoplpush命令前,先看一下mykey中链表的元素有哪些,注意他们的位置关系。
1) "a"
2) "b"
3) "c"
4) "d"
5) "e"
127.0.0.1:6379> RPOP mykey						    #移除并返回mykey键的第一个元素,从右取
"e"
127.0.0.1:6379> LRANGE mykey 0 -1
1) "a"
2) "b"
3) "c"
4) "d"
redis 127.0.0.1:6379> rpoplpush mykey mykey2	    #将mykey的尾部元素d弹出,同时再插入到mykey2的头部(原子性的完成这两步操作)。
"d"
redis 127.0.0.1:6379> lrange mykey 0 -1			    #通过lrange命令查看mykey在弹出尾部元素后的结果。
1) "a"
2) "b"
3) "c"
redis 127.0.0.1:6379> lrange mykey2 0 -1		    #通过lrange命令查看mykey2在插入元素后的结果。
1) "d"
redis 127.0.0.1:6379> rpoplpush mykey mykey		    #将source和destination设为同一键,将mykey中的尾部元素移到其头部。
"c"
redis 127.0.0.1:6379> lrange mykey 0 -1			    #查看移动结果。
1) "c"
2) "a"
3) "b"

Insert picture description here

Insert picture description here

3. Hash data type (hash type)

  • 概述:hash用于存储对象。可以采用这样的命名方式:对象类别和ID构成键名,使用字段表示对象的属性,而字段值则存储属性值。 如:存储 ID 为 2 的汽车对象。
    如果Hash中包含很少的字段,那么该类型的数据也将仅占用很少的磁盘空间。每一个Hash可以存储4294967295个键值对。

1. HSET/HGET/HDEL/HEXISTS/HLEN/HSETNX

命令 解释
HSET 为键设置字段与值
HGET 获取键包含的字段的值
HDEL 删除键中的字段,删除成功返回1,删除失败返回0
HEXISTS 判断键中是否存在字段,存在返回1,不存在返回0
HLEN 获取键中字段的数量
HSETNX 为键添加新字段,添加成功返回1,如果该字段存在,则不做任何操作返回0
redis 127.0.0.1:6379> hset myhash field1 "zhang"		#给键值为myhash的键设置字段为field1,值为zhang。
(integer) 1
redis 127.0.0.1:6379> hget myhash field1				#获取键值为myhash,字段为field1的值。
"zhang"
redis 127.0.0.1:6379> hget myhash field2				#myhash键中不存在field2字段,因此返回nil。
(nil)
redis 127.0.0.1:6379> hset myhash field2 "san"			#给myhash添加一个新的字段field2,其值为san。
(integer) 1
redis 127.0.0.1:6379> hlen myhash						#hlen命令获取myhash键的字段数量。
(integer) 2
redis 127.0.0.1:6379> hexists myhash field1			    #判断myhash键中是否存在字段名为field1的字段,由于存在,返回值为1。
(integer) 1
redis 127.0.0.1:6379> hdel myhash field1			    #删除myhash键中字段名为field1的字段,删除成功返回1。
(integer) 1
redis 127.0.0.1:6379> hdel myhash field1		        #再次删除myhash键中字段名为field1的字段,由于上一条命令已经将其删除,因为没有删除,返回0。
(integer) 0
redis 127.0.0.1:6379> hexists myhash field1		        #判断myhash键中是否存在field1字段,由于上一条命令已经将其删除,因为返回0。
(integer) 0
redis 127.0.0.1:6379> hsetnx myhash field1 zhang		#通过hsetnx命令给myhash添加新字段field1,其值为zhang,因为该字段已经被删除,所以该命令添加成功并返回1。
(integer) 1
redis 127.0.0.1:6379> hsetnx myhash field1 zhang		#由于myhash的field1字段已经通过上一条命令添加成功,因为本条命令不做任何操作后返回0。
(integer) 0

Insert picture description here

2. HINCRBY

命令 解释
HINCRBY 为键中字段的值追加整数值,如果原值不为整数则报错
redis 127.0.0.1:6379> del myhash					    #删除该键,便于后面示例的测试。
(integer) 1
redis 127.0.0.1:6379> hset myhash field 5			    #准备测试数据,该myhash的field字段设定值1。
(integer) 1
redis 127.0.0.1:6379> hincrby myhash field 1		    #hincrby命令给myhash的field字段的值加1,返回加后的结果。
(integer) 6
redis 127.0.0.1:6379> hincrby myhash field -1		    #hincrby命令给myhash的field字段的值加-1,返回加后的结果。
(integer) 5
redis 127.0.0.1:6379> hincrby myhash field -10		    #hincrby命令给myhash的field字段的值加-10,返回加后的结果。
(integer) -5  

Insert picture description here

3.HGETALL/HKEYS/HVALS/HMGET/HMSET

命令 解释
HGETALL 返回键中所有字段及其值,并且逐对列出
HKEYS 获取键中所有字段的名字
HVALS 获取键中所有字段的值
HMGET 获取键中多个字段,如果字段不存在则返回(nil)
HMSET 为键一次性设置多个字段
redis 127.0.0.1:6379> del myhash					               #删除该键,便于后面示例测试。
(integer) 1
redis 127.0.0.1:6379> hmset myhash field1 "hello" field2 "world"   #hmset命令为该键myhash,一次性设置多个字段,分别是field1="hello", field2="world"。
OK
redis 127.0.0.1:6379> hmget myhash field1 field2 field3		       #hmget命令获取myhash键的多个字段,其中field3并不存在,因为在返回结果中与该字段对应的值为nil。
1) "hello"
2) "world"
3) (nil)
redis 127.0.0.1:6379> hgetall myhash	    #hgetall命令返回myhash键的所有字段及其值,从结果中可以看出,他们是逐对列出的。
1) "field1"
2) "hello"
3) "field2"
4) "world"
redis 127.0.0.1:6379> hkeys myhash			#hkeys命令仅获取myhash键中所有字段的名字。
1) "field1"
2) "field2"
redis 127.0.0.1:6379> hvals myhash			#hvals命令仅获取myhash键中所有字段的值。
1) "hello"
2) "world" 

Insert picture description here

四.set数据类型(无序集合)

  • 概述:无序集合,元素类型为String类型,元素具有唯一性,不允许存在重复的成员。多个集合类型之间可以进行并集、交集和差集运算。
    应用范围:
  • 1.可以使用Redis的Set数据类型跟踪一些唯一性数据,比如访问某一博客的唯一IP地址信息。对于此场景,我们仅需在每次访问该博客时将访问者的IP存入Redis中,Set数据类型会自动保证IP地址的唯一性。
  • 2.充分利用Set类型的服务端聚合操作方便、高效的特性,可以用于维护数据对象之间的关联关系。比如所有购买某一电子设备的客户ID被存储在一个指定的Set中,而购买另外一种电子产品的客户ID被存储在另外一个Set中,如果此时我们想获取有哪些客户同时购买了这两种商品时,Set的intersections命令就可以充分发挥它的方便和效率的优势了。

1.SADD/SMEMBERS/SCARD/SISMEMBER

命令 解释
SADD 向键中插入元素,如果键中已经存在将要插入的参数则无法插入
SMEMBERS 查看插入结果,输出的顺序和插入顺序无关,随机显示
SCARD 获取键中元素数量
SISMEMBER 判断键中元素是否存在,存在返回1,不存在返回0
redis 127.0.0.1:6379> sadd myset a b c		#插入测试数据,由于该键myset之前并不存在,因此参数中的三个成员都被正常插入。
(integer) 3
redis 127.0.0.1:6379> sadd myset a d e		#由于参数中的a在myset中已经存在,因此本次操作仅仅插入了d和e两个新成员。
(integer) 2
redis 127.0.0.1:6379> sismember myset a		#判断a是否已经存在,返回值为1表示存在。
(integer) 1
redis 127.0.0.1:6379> sismember myset f		#判断f是否已经存在,返回值为0表示不存在。
(integer) 0
redis 127.0.0.1:6379> smembers myset		#通过smembers命令查看插入的结果,从结果可以看出,输出的顺序和插入顺序无关。
1) "c"
2) "d"
3) "a"
4) "b"
5) "e"
redis 127.0.0.1:6379> scard myset			#获取Set键中元素的数量。
(integer) 5

Insert picture description here

2.SPOP/SREM/SRANDMEMBER/SMOVE

命令 解释
SPOP 随机移除并返回键中的一个成员
SREM 从键中移除成员,如果移除的成员本不存在,只会移除存在的成员并返回移除的数量
SRANDMEMBER 随机返回键中的某一个成员
SMOVE 将原键中的一个成员移动到新键中,执行成功返回1
redis 127.0.0.1:6379> del myset				    #删除该键,便于后面的测试。
(integer) 1
redis 127.0.0.1:6379> sadd myset a b c d	    #为后面的示例准备测试数据。
(integer) 4
redis 127.0.0.1:6379> smembers myset		    #查看Set中成员的位置。
1) "c"
2) "a"
3) "d"
4) "b"
redis 127.0.0.1:6379> srandmember myset		    #从结果可以看出,该命令确实是随机的返回了某一成员。
"a"
redis 127.0.0.1:6379> spop myset			    #随机的移除并返回Set中的某一成员。
"b"
redis 127.0.0.1:6379> smembers myset		    #查看移出后Set的成员信息。
1) "c"
2) "a"
3) "d"
redis 127.0.0.1:6379> srem myset a d f	        #从Set中移出a、d和f三个成员,其中f并不存在,因此只有a和d两个成员被移出,返回为2。
(integer) 2
redis 127.0.0.1:6379> smembers myset		    #查看移出后的输出结果。
1) "c"
redis 127.0.0.1:6379> sadd myset a b		    #为后面的smove命令准备数据。
(integer) 2
redis 127.0.0.1:6379> sadd myset2 c d
(integer) 2
redis 127.0.0.1:6379> smove myset myset2 a		#将a从myset移到myset2,从结果可以看出移动成功。
(integer) 1
redis 127.0.0.1:6379> smove myset myset2 a		#再次将a从myset移到myset2,由于此时a已经不是myset的成员了,因此移动失败并返回0。
(integer) 0
redis 127.0.0.1:6379> smembers myset			#分别查看myset和myset2的成员,确认移动是否真的成功。
1) "c"
2) "b"
redis 127.0.0.1:6379> smembers myset2
1) "a"
2) "c"
3) "d"

Insert picture description here
Insert picture description here

五.Sorted Set数据类型(zset、有序集合)

  • 概述:a、有序集合,元素类型为Sting,元素具有唯一性,不能重复。
    b、每个元素都会关联一个double类型的分数score(表示权重),可以通过权重的大小排序,元素的score可以相同。

  • 应用范围:

  • 1.可以用于一个大型在线游戏的积分排行榜。每当玩家的分数发生变化时,可以执行ZADD命令更新玩家的分数,此后再通过ZRANGE命令获取积分TOP10的用户信息。当然我们也可以利用ZRANK命令通过username来获取玩家的排行信息。最后我们将组合使用ZRANGE和ZRANK命令快速的获取和某个玩家积分相近的其他用户的信息。

  • 2.Sorted-Set类型还可用于构建索引数据。

1. ZADD/ZCARD/ZCOUNT/ZREM/ZINCRBY/ZSCORE/ZRANGE/ZRANK

命令 解释
ZADD 添加一个成员并赋予值
ZCARD Get the number of members in the key
ZCOUNT Display the number of members that meet the specified value of the expression
ZREM Delete one or more members and return the deleted quantity
ZINCRBY If the member does not exist, assume the value of the member is 0, and add or reduce the corresponding value and return the result, if the member exists, directly add or reduce the value and return the result
ZSCORE Get the value of the member in the key and return it as a string, or "nil" if the member does not exist
ZRANGE Return members, when used with WITHSCORES, members and their values ​​will be returned
ZRANK Get the position index value of the member in the key, 0 means the first position, if the member does not exist, return "nil"
redis 127.0.0.1:6379> zadd myzset 1 "one"				#添加一个分数为1的成员。
(integer) 1
redis 127.0.0.1:6379> zadd myzset 2 "two" 3 "three"		#添加两个分数分别是2和3的两个成员。
(integer) 2
redis 127.0.0.1:6379> zrange myzset 0 -1 WITHSCORES		#0表示第一个成员,-1表示最后一个成员。WITHSCORES选项表示返回的结果中包含每个成员及其分数,否则只返回成员。
1) "one"
2) "1"
3) "two"
4) "2"
5) "three"
6) "3"
redis 127.0.0.1:6379> zrank myzset one					#获取成员one在Sorted-Set中的位置索引值。0表示第一个位置。
(integer) 0
redis 127.0.0.1:6379> zrank myzset four					#成员four并不存在,因此返回nil。
(nil)
redis 127.0.0.1:6379> zcard myzset						#获取myzset键中成员的数量。
(integer) 3
redis 127.0.0.1:6379> zcount myzset 1 2					#zcount key min max,分数满足表达式1 <= score <= 2的成员的数量。
(integer) 2
redis 127.0.0.1:6379> zrem myzset one two				#删除成员one和two,返回实际删除成员的数量。
(integer) 2
redis 127.0.0.1:6379> zcard myzset						#查看是否删除成功。
(integer) 1
redis 127.0.0.1:6379> zscore myzset three				#获取成员three的分数。返回值是字符串形式。
"3"
redis 127.0.0.1:6379> zscore myzset two					#由于成员two已经被删除,所以该命令返回nil。
(nil)
redis 127.0.0.1:6379> zincrby myzset 2 one	            #成员one不存在,zincrby命令将添加该成员并假设其初始分数为0,将成员one的分数增加2,并返回该成员更新后的分数。
"2"
redis 127.0.0.1:6379> zincrby myzset -1 one				#将成员one的分数增加-1,并返回该成员更新后的分数。
"1"
redis 127.0.0.1:6379> zrange myzset 0 -1 WITHSCORES		#查看在更新了成员的分数后是否正确。
1) "one"
2) "1"
3) "three"
4) "3"

Insert picture description here
Insert picture description here

2. ZRANGEBYSCORE/ZREMRANGEBYRANK/ZREMRANGEBYSCORE

command Explanation
ZRANGEBYSCORE Get the value that satisfies the value between the specified value of the expression
ZREMRANGEBYRANK Delete members whose index position satisfies the value specified by the expression
ZREMRANGEBYSCORE The deleted value satisfies the direct value specified by the expression, and returns the actual deleted number
redis 127.0.0.1:6379> del myzset
(integer) 1
redis 127.0.0.1:6379> zadd myzset 1 one 2 two 3 three 4 four
(integer) 4
redis 127.0.0.1:6379> zrangebyscore myzset 1 2		                #zrangebyscore key min max,获取分数满足表达式1 <= score <= 2的成员。
1) "one"
2) "two"
redis 127.0.0.1:6379> zrangebyscore myzset (1 2			            #获取分数满足表达式1 < score <= 2的成员。
1) "two"
redis 127.0.0.1:6379> zrangebyscore myzset -inf +inf limit 2 3		#-inf表示第一个成员,+inf表示最后一个成员,limit后面的参数用于限制返回成员的自己,2表示从位置索引等于2的成员开始,取后面3个成员。
1) "three"
2) "four"
redis 127.0.0.1:6379> zremrangebyscore myzset 1 2		            #删除分数满足表达式1 <= score <= 2的成员,并返回实际删除的数量。
(integer) 2
redis 127.0.0.1:6379> zrange myzset 0 -1				            #看出一下上面的删除是否成功。
1) "three"
2) "four"
redis 127.0.0.1:6379> zremrangebyrank myzset 0 1		            #删除位置索引满足表达式0 <= rank <= 1的成员。
(integer) 2
redis 127.0.0.1:6379> zcard myzset						            #查看上一条命令是否删除成功。
(integer) 0

Insert picture description here

3. ZREVRANGE / ZREVRANGEBYSCORE / ZREVRANK

command Explanation
ZREVRANGE The position index is obtained from the highest to the bottom and returns the members in the specified range
ZREVRANGEBYSCORE View the members contained in the specified index position range, and sort them from high to bottom
ZREVRANK View the value of the specified index, the position is sorted from high to low
redis 127.0.0.1:6379> del myzset						            #为后面的示例准备测试数据。
(integer) 0
redis 127.0.0.1:6379> zadd myzset 1 one 2 two 3 three 4 four
(integer) 4
redis 127.0.0.1:6379> zrevrange myzset 0 -1 WITHSCORES		        #以位置索引从高到低的方式获取并返回此区间内的成员。
1) "four"
2) "4"
3) "three"
4) "3"
5) "two"
6) "2"
7) "one"
8) "1"
redis 127.0.0.1:6379> zrevrange myzset 1 3			                #由于是从高到低的排序,所以位置等于0的是four,1是three,并以此类推。
1) "three"
2) "two"
3) "one"
redis 127.0.0.1:6379> zrevrank myzset one			                #由于是从高到低的排序,所以one的位置是3。
(integer) 3
redis 127.0.0.1:6379> zrevrank myzset four			                #由于是从高到低的排序,所以four的位置是0。
(integer) 0
redis 127.0.0.1:6379> zrevrangebyscore myzset 3 0	                #zrevrangebyscore key max min, 获取分数满足表达式3 >= score >= 0 的成员,并以从高到底的顺序输出。
1) "three"
2) "two"
3) "one"
redis 127.0.0.1:6379> zrevrangebyscore myzset 4 0 limit 1 2		    #zrevrangebyscore命令支持limit选项,其含义等同于zrangebyscore中的该选项,只是在计算位置时按照相反的顺序计算和获取。
1) "three"
2) "two"

Insert picture description here

Guess you like

Origin blog.csdn.net/LI_MINGXUAN/article/details/114019827