Redis Set(無序集合)

2021-10-14 09:33:22 字數 4235 閱讀 6792

127.0

.0.1:6379

> sadd myset "hello"

# set集合中新增元素

(integer)1

127.0

.0.1:6379

> sadd myset "kuangshen"

(integer)1

127.0

.0.1:6379

> sadd myset "lovekuangshen"

(integer)1

127.0

.0.1:6379

> smembers myset # 檢視指定set的所有值 1)

"hello"2)

"lovekuangshen"3)

"kuangshen"

127.0

.0.1:6379

> sismember myset hello # 判斷某乙個值是不是在set集合中!

(integer)1

127.0

.0.1:6379

> sismember myset world

(integer)0

127.0

.0.1:6379

> scard myset # 獲取set集合中的內容元素個數!

(integer

)4

127.0

.0.1:6379

> srem myset hello # 移除set集合中的指定元素

(integer)1

127.0

.0.1:6379

> scard myset

(integer)3

127.0

.0.1:6379

> smembers myset 1)

"lovekuangshen2"2)

"lovekuangshen"3)

"kuangshen"

set 無序不重複集合。抽隨機! 

127.0

.0.1:6379

> smembers myset 1)

"lovekuangshen2"2)

"lovekuangshen"3)

"kuangshen"

127.0

.0.1:6379

> srandmember myset # 隨機抽選出乙個元素

"kuangshen"

127.0

.0.1:6379

> srandmember myset # 隨機抽選出乙個元素

"lovekuangshen2"

127.0

.0.1:6379

> srandmember myset 2

# 隨機抽選出指定個數的元素1)

"lovekuangshen"2)

"lovekuangshen2"

127.0

.0.1:6379

> srandmember myset 21)

"kuangshen"2)

"lovekuangshen2"

127.0

.0.1:6379

> lrange list 0-1

1)"three"2)

"three"3)

"two"4)

"one"

127.0

.0.1:6379

> lrem list 1 one # 移除list集合中第乙個one的值,精確匹配

(integer)1

127.0

.0.1:6379

> lrange list 0-1

1)"three"2)

"three"3)

"two"

127.0

.0.1:6379

> lrem list 2 three # 移除list集合中前兩個three的值,精確匹配

(integer)2

127.0

.0.1:6379

> lrange list 0-1

1)"two"

127.0

.0.1:6379

> smembers myset 1)

"lovekuangshen2"2)

"lovekuangshen"3)

"kuangshen"

127.0

.0.1:6379

> spop myset # 隨機刪除一些set集合中的元素!

"lovekuangshen2"

127.0

.0.1:6379

> spop myset

"lovekuangshen"

127.0

.0.1:6379

> smembers myset 1)

"kuangshen"

127.0

.0.1:6379

> sadd myset "hello"

(integer)1

127.0

.0.1:6379

> sadd myset "world"

(integer)1

127.0

.0.1:6379

> sadd myset "kuangshen"

(integer)1

127.0

.0.1:6379

> sadd myset2 "set2"

(integer)1

127.0

.0.1:6379

> smove myset myset2 "kuangshen"

# 將乙個指定的值,移動到另外乙個set集合!

(integer)1

127.0

.0.1:6379

> smembers myset 1)

"world"2)

"hello"

127.0

.0.1:6379

> smembers myset2 1)

"kuangshen"2)

"set2"

key1:a,b,c    key2: c,d,e

127.0

.0.1:6379

> sdiff key1 key2 # 差集 1)

"b"2

)"a"

127.0

.0.1:6379

> sinter key1 key2 # 交集 共同好友就可以這樣實現 1)

"c"127.0

.0.1:6379

> sunion key1 key2 # 並集 1)

"b"2

)"c"3)

"e"4

)"a"5)

"d"

127.0

.0.1:6379

> rpush mylist "hello"

(integer)1

127.0

.0.1:6379

> rpush mylist "world"

(integer)2

127.0

.0.1:6379

> linsert mylist before "world"

"other"

# 在world前插入乙個 other

(integer)3

127.0

.0.1:6379

> lrange mylist 0-1

1)"hello"2)

"other"3)

"world"

127.0

.0.1:6379

> linsert mylist after world new # 在world後插入乙個 new

(integer)4

127.0

.0.1:6379

> lrange mylist 0-1

1)"hello"2)

"other"3)

"world"4)

"new"

C 操作Redis Set 無序集合

1 2 redis 無序集合 3 4public static void redis set 5 item 出來的結果是無須的21 2223 client.additemtoset set2 lilei 24 client.additemtoset set2 liming 25 client.add...

Redis set集合常用常用指令

sadd scard srem smember 檢視指定集合的所有元素 srandmember 隨機抽取指定元素的乙個值 srandmember 隨機抽取指定元素的多個值 spop smove 127.0.0.1 6379 sadd set1 2 3 4 5 6 1 設定集合元素 integer 6...

redis學習 五 redis set集合操作

1 向set集合新增元素 sadd key value1 value2 2 刪除set集合的元素 srem key value 3 pop出乙個隨機元素 spop key 4 返回乙個隨機元素,但並不刪除 srandmember key 5 檢視所有元素 smembers key 6 判斷某個元素是...