Redis(六):Set集合数据类型详解
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                Redis(六):Set集合数据类型详解
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                Redis 的 Set 是 String 類型的無序集合。集合成員是唯一的,這就意味著集合中不能出現重復的數據。
Redis 中集合是通過哈希表實現的,所以添加,刪除,查找的復雜度都是 O(1)。
集合中最大的成員數為 232 - 1 (4294967295, 每個集合可存儲40多億個成員)。
實例:
redis 127.0.0.1:6379> SADD runoobkey redis (integer) 1 redis 127.0.0.1:6379> SADD runoobkey mongodb (integer) 1 redis 127.0.0.1:6379> SADD runoobkey mysql (integer) 1 redis 127.0.0.1:6379> SADD runoobkey mysql (integer) 0 redis 127.0.0.1:6379> SMEMBERS runoobkey1) "mysql" 2) "mongodb" 3) "redis"1、Set常用命令
1.1、SADD
127.0.0.1:6379> sadd myset "hello" //set集合添加元素 (integer) 1 127.0.0.1:6379> sadd myset "lizhengi" (integer) 1 127.0.0.1:6379> sadd myset "world" (integer) 1 127.0.0.1:6379> SMEMBERS myset //查看指定set所有值 1) "world" 2) "lizhengi" 3) "hello" 127.0.0.1:6379> SISMEMBER myset hello //判斷某個值是否在set中 (integer) 1 127.0.0.1:6379> SISMEMBER myset hh (integer) 01.2、scard
127.0.0.1:6379> scard myset //獲取set長度 (integer) 31.3、srem
127.0.0.1:6379> SMEMBERS myset 1) "world" 2) "lizhengi" 3) "hello" 127.0.0.1:6379> srem myset hello world //移除set集合中指定一些元素 (integer) 2 127.0.0.1:6379> scard myset (integer) 1 127.0.0.1:6379> SMEMBERS myset 1) "lizhengi" 127.0.0.1:6379>1.4、SRANDMEMBER
127.0.0.1:6379> SRANDMEMBER myset //隨機抽出set集合的一個元素 "hello" 127.0.0.1:6379> SRANDMEMBER myset "world" 127.0.0.1:6379>1.5、SPOP
127.0.0.1:6379> SMEMBERS myset 1) "adsa" 2) "world" 3) "lizhengi" 4) "hello" 127.0.0.1:6379> SPOP myset //隨機刪除set集合某些元素 "lizhengi" 127.0.0.1:6379> SPOP myset "hello" 127.0.0.1:6379> SMEMBERS myset 1) "adsa" 2) "world" 127.0.0.1:6379>1.6、SMOVE
127.0.0.1:6379> SMEMBERS myset 1) "adsa" 2) "world" 127.0.0.1:6379> sadd myset2 "adsa" (integer) 1 127.0.0.1:6379> SMEMBERS myset 1) "adsa" 2) "world" 127.0.0.1:6379> SMEMBERS myset2 1) "adsa" 127.0.0.1:6379> SMOVE myset myset2 "world" //移動指定元素到另外一個集合 (integer) 1 127.0.0.1:6379> SMEMBERS myset 1) "adsa" 127.0.0.1:6379> SMEMBERS myset2 1) "adsa" 2) "world" 127.0.0.1:6379>1.7、SDIFF 、SINTER、SUNION
127.0.0.1:6379> SMEMBERS myset 1) "adsa" 2) "aadsa" 127.0.0.1:6379> SMEMBERS myset2 1) "adsa" 2) "world" 127.0.0.1:6379> SDIFF myset myset2 //差集 1) "aadsa" 127.0.0.1:6379> SINTER myset myset2 //交集 1) "adsa" 127.0.0.1:6379> SUNION myset myset2 //并集 1) "adsa" 2) "world" 3) "aadsa"總結
以上是生活随笔為你收集整理的Redis(六):Set集合数据类型详解的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: xtrabackup备份脚本
 - 下一篇: Redis(二):Redis入门与性能测