Redis 学习笔记Set操作
Posted Adorable_Rocy
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Redis 学习笔记Set操作相关的知识,希望对你有一定的参考价值。
1.sadd key val
127.0.0.1:6379> sadd list hello # 添加内容
(integer) 1
127.0.0.1:6379> sadd list world
(integer) 1
127.0.0.1:6379>
2.smembers key
127.0.0.1:6379> SMEMBERS list # 查看内容
1) "world"
2) "hello"
127.0.0.1:6379>
3.sismember key val
127.0.0.1:6379> SISMEMBER list hello
(integer) 1
127.0.0.1:6379> SISMEMBER list welcome # 判断key中是否含有这个值
(integer) 0
127.0.0.1:6379>
4.scard key
127.0.0.1:6379> SCARD list # 查看元素总数
(integer) 2
127.0.0.1:6379>
5.srem key val
127.0.0.1:6379> SCARD list
(integer) 2
127.0.0.1:6379> srem list haha # 删除不存在元素,不会抛出异常
(integer) 0
127.0.0.1:6379> srem list hello # 删除指定元素hello
(integer) 1
127.0.0.1:6379> SMEMBERS list
1) "world"
6.srandmember key [count]
127.0.0.1:6379> SMEMBERS list
1) "v3"
2) "v4"
3) "v5"
4) "v2"
5) "v1"
6) "v6"
127.0.0.1:6379> SRANDMEMBER list # 生成随机数
"v6"
127.0.0.1:6379> SRANDMEMBER list
"v5"
127.0.0.1:6379> SRANDMEMBER list
"v2"
127.0.0.1:6379> SRANDMEMBER list
"v5"
127.0.0.1:6379> SRANDMEMBER list 3 # 生成指定个数随机数
1) "v5"
2) "v2"
3) "v4"
127.0.0.1:6379> SRANDMEMBER list 3
1) "v2"
2) "v6"
3) "v3"
127.0.0.1:6379> SRANDMEMBER list 3
1) "v5"
2) "v1"
3) "v3"
127.0.0.1:6379> SRANDMEMBER list 3
1) "v1"
2) "v3"
3) "v4"
7.spop key val
127.0.0.1:6379> SMEMBERS list
1) "v5"
2) "v2"
3) "v1"
4) "v6"
5) "v3"
6) "v4"
127.0.0.1:6379> spop list 3 # 随机删除3个值
1) "v1"
2) "v2"
3) "v4"
127.0.0.1:6379> SMEMBERS list
1) "v5"
2) "v6"
3) "v3"
127.0.0.1:6379> spop list # 随机删除一个值
"v6"
127.0.0.1:6379> spop list
"v3"
127.0.0.1:6379> spop list
"v5"
127.0.0.1:6379> SMEMBERS list
(empty array)
127.0.0.1:6379>
8.smove oldKey newKey val
127.0.0.1:6379> SMEMBERS list
1) "v3"
2) "v2"
3) "v1"
4) "v4"
5) "v5"
127.0.0.1:6379> SMOVE list mylist v3 # 移动list的v3元素到mylist中
(integer) 1
127.0.0.1:6379> SMEMBERS list
1) "v1"
2) "v4"
3) "v5"
4) "v2"
127.0.0.1:6379> SMEMBERS mylist
1) "v3"
127.0.0.1:6379>
9.sdiff k1 k2 | sinter k1 k2 | sunion k1 k2
127.0.0.1:6379> SMEMBERS k1
1) "v3"
2) "v2"
3) "v1"
127.0.0.1:6379> SMEMBERS k2
1) "v5"
2) "v3"
3) "v4"
127.0.0.1:6379> SDIFF k1 k2 # 差集,以 k1 为参考 剔除共同的元素
1) "v2"
2) "v1"
127.0.0.1:6379> SINTER k1 k2 # 并集,共同元素
1) "v3"
127.0.0.1:6379> SUNION k1 k2 # 合集,合并元素
1) "v2"
2) "v3"
3) "v1"
4) "v5"
5) "v4"
127.0.0.1:6379>
以上是关于Redis 学习笔记Set操作的主要内容,如果未能解决你的问题,请参考以下文章
[原创]java WEB学习笔记61:Struts2学习之路--通用标签 property,uri,param,set,push,if-else,itertor,sort,date,a标签等(代码片段