本文讲解redis常用数据类型集合Set的基本操作与数据结构
1. 基本操作
# sadd集合添加元素
127.0.0.1:6379> sadd username shenjian shenjian domi jing
(integer) 3
# smembers取出集合中所有元素
127.0.0.1:6379> smembers username
1) "shenjian"
2) "domi"
3) "jing"
# sismember查看元素是否在集合中,1是 0否
127.0.0.1:6379> sismember username shenjian
(integer) 1
# scard返回集合大小
127.0.0.1:6379> scard username
(integer) 3
# srem删除集合中指定元素
127.0.0.1:6379> srem username jing
(integer) 1
# spop随机从结合中抛出n个数元素
127.0.0.1:6379> spop username 1
1) "domi"
127.0.0.1:6379> sadd username zhangsan lisi
(integer) 2
# srandmember从集合中随机取出n个值,但不会删除
127.0.0.1:6379> srandmember username 1
1) "shenjian"
127.0.0.1:6379> sadd age 20
(integer) 1
# smove <source> <destination> member把源集合中元素移动到目标集合中
127.0.0.1:6379> smove username age shenjian
(integer) 1
127.0.0.1:6379> smembers age
1) "shenjian"
2) "20"
127.0.0.1:6379> sadd username shenjian
(integer) 1
# sinter取集合交集
127.0.0.1:6379> sinter username age
1) "shenjian"
# sunion取集合并集
127.0.0.1:6379> sunion username age
1) "shenjian"
2) "zhangsan"
3) "20"
4) "lisi"
# sdiff取集合差集
127.0.0.1:6379> sdiff username age
1) "zhangsan"
2) "lisi"
2. 数据结构
127.0.0.1:6379> object encoding username
"hashtable"
127.0.0.1:6379> sadd number 1 2 3
(integer) 3
127.0.0.1:6379> object encoding number
"intset"
127.0.0.1:6379> sadd number shen
(integer) 1
127.0.0.1:6379> object encoding number
"hashtable"
由上可知,Set底层数据结构为整数集合intset和字典hashtable,其中intset可以理解为一种特殊的数组,而hashtable就是普通的哈希表
标签:username,Set,0.1,数据类型,Redis,6379,127.0,integer,shenjian From: https://www.cnblogs.com/shenjian-online/p/16845755.html