新人入坑Redis必会的吐血总结 (3)

example:

127.0.0.1:6379> linsert score1 before 3 aaa (integer) 3 127.0.0.1:6379> lrange score1 0 -1 1) "1" 2) "aaa" 3) "3" 127.0.0.1:6379> linsert score1 after 3 bbb (integer) 4 127.0.0.1:6379> lrange score1 0 -1 1) "1" 2) "aaa" 3) "3" 4) "bbb"

rpoplpush resource destination:将链表中的尾部元素弹出并添加到头部。[循环操作]

example:

127.0.0.1:6379> lrange score1 0 -1 1) "1" 2) "aaa" 3) "3" 127.0.0.1:6379> lrange score2 0 -1 1) "bbb" 2) "1" 3) "4" 127.0.0.1:6379> rpoplpush score1 score2 "3" 127.0.0.1:6379> lrange score1 0 -1 1) "1" 2) "aaa" 127.0.0.1:6379> lrange score2 0 -1 1) "3" 2) "bbb" 3) "1" 4) "4" 4、Redis-set 赋值

sadd key value[value1 value2...]:向set中添加元素,如果该key的值已有则不会重复添加

example:

127.0.0.1:6379> sadd abc 1 2 3 4 (integer) 4

srem key member[member1 member2...]:删除set中指定的成员

example:

127.0.0.1:6379> srem abc 1 3 (integer) 2 取值

smembers key:获取set中所有的成员

example:

127.0.0.1:6379> smembers abc 1) "2" 2) "4"

sismember key member:判断参数中指定的成员是否在该set中,1表示存在,0表示不存在或者该key本身就不存在(无论集合中有多少元素都可以极速的返回结果)

example:

127.0.0.1:6379> sismember abc 2 (integer) 1 127.0.0.1:6379> sismember abc 3 (integer) 0 删值

集合运算

差集运算

sdiff key1 key2...:返回key1与key2中相差的成员,而且与key的顺序有关,即返回差集

example:

127.0.0.1:6379> sadd set1 1 2 3 4 5 (integer) 5 127.0.0.1:6379> sadd set2 3 4 5 6 7 (integer) 5 127.0.0.1:6379> sdiff set1 set2 1) "1" 2) "2" 127.0.0.1:6379> sdiff set2 set1 1) "6" 2) "7"

交集运算

sinter key1 key2...:返回交集

example:

127.0.0.1:6379> sinter set1 set2 1) "3" 2) "4" 3) "5"

并集运算

sunion key1 key2...:返回并集

example:

127.0.0.1:6379> sunion set1 set2 1) "1" 2) "2" 3) "3" 4) "4" 5) "5" 6) "6" 7) "7" 扩展命令

scard key:获取set中成员的数量

example:

127.0.0.1:6379> scard set1 (integer) 5

srandmember key:随机返回set中的一个成员

example:

127.0.0.1:6379> srandmember set1 "1" 127.0.0.1:6379> srandmember set1 "5" 127.0.0.1:6379> srandmember set1 "3"

sdiffstore destination key1 key2...:将key1 key2...相差的成员存储在destination上

example:

127.0.0.1:6379> sdiffstore set3 set1 set2 (integer) 2 127.0.0.1:6379> smembers set3 1) "1" 2) "2"

sinterstore destination key[key...]:将返回的交集存储在destination上

example:

127.0.0.1:6379> sinterstore set4 set1 set2 (integer) 3 127.0.0.1:6379> smembers set4 1) "3" 2) "4" 3) "5" 5、Redis-sortedset 赋值

zadd key score member score2 member2...:将所有成员以及该成员的分数存放到sorted-set中。如果该元素已经存在则会用新的分数代替原有的分数,返回值是新加入到集合中的元素个数,不包含之前已经存在的元素。

example:

127.0.0.1:6379> zadd list 50 xiaoming 100 xiaohong 200 xiaozhang (integer) 3 取值

zscore key member:返回指定成员的分数

example:

127.0.0.1:6379> zscore list xiaohong "100"

zcard key:获取集合中的成员数量

example:

127.0.0.1:6379> zcard list (integer) 3 范围查询

zrange key start end[withscores]:获取集合中脚标为start-end的成员,[withscores]参数表明返回的成员包含其分数(分数从小到大排序)

example:

127.0.0.1:6379> zrange list 0 -1 1) "xiaoming" 2) "xiaohong" 3) "xiaozhang" 127.0.0.1:6379> zrange list 0 -1 withscores 1) "xiaoming" 2) "50" 3) "xiaohong" 4) "100" 5) "xiaozhang" 6) "200"

zrevrange key start end[withscores]:获取集合中脚标为start-end的成员,[withscores]参数表明返回的成员包含其分数(分数从大到小排序)

example:

127.0.0.1:6379> zrevrange list 0 -1 1) "xiaozhang" 2) "xiaohong" 3) "xiaoming" 127.0.0.1:6379> zrevrange list 0 -1 withscores 1) "xiaozhang" 2) "200" 3) "xiaohong" 4) "100" 5) "xiaoming" 6) "50" 删值

zrem key member[member...]:移除集合中指定的成员,可以指定多个成员

example:

127.0.0.1:6379> zrem list xiaozhang (integer) 1

zremrangebyrank key start stop:按照排名范围删除元素

example:

127.0.0.1:6379> zremrangebyrank list 0 1 (integer) 2

zremrangebyscore key min max:按照分数范围删除元素

example:

127.0.0.1:6379> zremrangebyscore list 50 120 (integer) 2 扩展命令

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zygzfd.html