Redis学习笔记(六)——数据结构之Set
一、介绍
Redis的Set是string类型的无序集合。集合成员是唯一的,这就意味着集合中不能出现重复的数据。
Redis中集合是通过哈希表实现的,所以添加、删除、查找的复杂度都是O(1)。
集合中最大的成员数为 (4294967295, 每个集合可存储40多亿个成员)。
基本命令:
- 增加/更新
sadd key member1 [member2 ...] (向集合添加一个或多个成员)
- 查询
scard key (获取集合的成员数)
smembers key (返回集合中的所有成员)
srandmember key [count] (返回集合中一个或多个随机数)
sscan key cursor [match pattern] [count count] (迭代集合中的元素)
- 判断
sismember key member (判断member元素是否是集合key的成员)
- 删除/移动
srem key member1 [member2 ...] (移除集合中一个或多个成员)
spop key (移除并返回集合中的一个随机元素)
smove source destination member (将member元素从source集合移动到destination集合)
- 差集/并集
sdiff key1 [key2] (返回给定所有集合的差集)
sdiffstore destination key1 [key2] (返回给定所有集合的差集并存储在destination中)
sinter key1 [key2] (返回给定所有集合的交集)
sinterstore destination key1 [key2] (返回给定所有集合的交集并存储在destination中)
sunion key1 [key2] (返回所有给定集合的并集)
sunionstore destination key1 [key2] (所有给定集合的并集存储在destination集合中)
二、操作
- 增加
1、sadd key member1 [member2 ...] (向集合添加一个或多个成员)
如果集合中已存在已有的元素,则添加返回0
- 查询
1、scard key (获取集合的成员数)
2、smembers key (返回集合中的所有成员)
3、srandmember key [count] (返回集合中一个或多个随机数)
4、sscan key cursor [match pattern] [count count] (迭代集合中的元素)
判断集合元素是否存在(0: 不存在, 1: 存在)
- 判断
1、sismember key member (判断member元素是否是集合key的成员)
- 删除/移动
1、srem key member1 [member2 ...] (移除集合中一个或多个成员)
如果删除的元素不存在,返回0,删除成功返回1
2、spop key [count] (移除并返回集合中的一个或多个随机元素)
3、smove source destination member (将member元素从source集合移动到destination集合)
如果集合中元素不存在,返回0
- 差集/并集
1、sdiff key1 [key2] (返回给定所有集合的差集)
获取两个集合的差集(不同的元素)
2、sdiffstore destination key1 [key2] (返回给定所有集合的差集并存储在destination中)
set1 和 set2 的差集
返回set1和set2集合的差集并存储在set3中:
3、sinter key1 [key2] (返回给定所有集合的交集)
获取两个集合的交集(相同的元素)
4、sinterstore destination key1 [key2] (返回给定所有集合的交集并存储在destination中)
5、sunion key1 [key2] (返回所有给定集合的并集)
并集的结果:给定的集合里面的元素只能出现一次
6、sunionstore destination key1 [key2] (所有给定集合的并集存储在destination集合中)
最新文章
- Android中Activity加入Fragment使用注意事项及常用技巧
- ADF_Desktop Integration系列4_ADF桌面集成入门之部署ADF Desktop Excel
- 判断iis是否已经安装
- php中函数前加&;符号的作用分解
- Android之路-------传说中的大喇叭(广播接收器)
- Hadoop运行中遇到的错误调试
- ASP.NET MVC IOC 之AutoFac
- TP5.x——update更新成功但是返回是0
- iOS.GetCurrentTimestamp
- 20155239 《网络对抗》Exp4 恶意代码分析
- 【Linux技术】autotools制作makefile过程详解
- JS 控制页面超时后自动跳转到登陆页面
- <; Python Index >;
- Memcache介绍与应用场景
- intercepting IO request
- ny14 会场安排问题
- InstaGAN: Instance-Aware Image-to-Image Translation
- Static Nested Class 和 Inner Class的不同?
- CentOS6.2 下安装OpenGL开发库
- bzoj 1597 [Usaco2008 Mar]土地购买——斜率优化dp