redis存储set分类

加入要对渠道进行分类:国内和海外(inland,overseas)

一种方式是最好在redis中见上述两个set key
然后使用 SISMEMBER key member 传入渠道特征作为member的值去判断它是否在一个集合中
如果两个集合必有一个有该渠道,只需要判断一次
否则循环所有集合的key,存在该成员则跳出循环

该类问题涉及到分类和存在性,exists方法只是检查该key是否存在,缺乏分类能力。

另一办法是使用key(渠道特征),string key的内容为国内或海外,可以exists和get 该key知道它的类别(set appsflyer overseas)

redis做随机抽奖,使用RANDOMKEY,添加key时使用所有人员的编号,在的用1做值否则用0做值。
randomkey返回人员编号再看其值有效性。


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 runoobkey

  1. "mysql"
  2. "mongodb"
  3. "redis"

下表列出了 Redis 集合基本命令:
序号 命令及描述
1 SADD key member1 [member2]
向集合添加一个或多个成员
2 SCARD key
获取集合的成员数
3 SDIFF key1 [key2]
返回给定所有集合的差集
4 SDIFFSTORE destination key1 [key2]
返回给定所有集合的差集并存储在 destination 中
5 SINTER key1 [key2]
返回给定所有集合的交集
6 SINTERSTORE destination key1 [key2]
返回给定所有集合的交集并存储在 destination 中
7 SISMEMBER key member
判断 member 元素是否是集合 key 的成员
8 SMEMBERS key
返回集合中的所有成员
9 SMOVE source destination member
将 member 元素从 source 集合移动到 destination 集合
10 SPOP key
移除并返回集合中的一个随机元素
11 SRANDMEMBER key [count]
返回集合中一个或多个随机数
12 SREM key member1 [member2]
移除集合中一个或多个成员
13 SUNION key1 [key2]
返回所有给定集合的并集
14 SUNIONSTORE destination key1 [key2]
所有给定集合的并集存储在 destination 集合中
15 SSCAN key cursor [MATCH pattern] [COUNT count]
迭代集合中的元素下表列出了 Redis 集合基本命令:

| 序号 | 命令及描述 |
| 1 | SADD key member1 [member2]
向集合添加一个或多个成员 |
| 2 | SCARD key
获取集合的成员数 |
| 3 | SDIFF key1 [key2]
返回给定所有集合的差集 |
| 4 | SDIFFSTORE destination key1 [key2]
返回给定所有集合的差集并存储在 destination 中 |
| 5 | SINTER key1 [key2]
返回给定所有集合的交集 |
| 6 | SINTERSTORE destination key1 [key2]
返回给定所有集合的交集并存储在 destination 中 |
| 7 | SISMEMBER key member
判断 member 元素是否是集合 key 的成员 |
| 8 | SMEMBERS key
返回集合中的所有成员 |
| 9 | SMOVE source destination member
将 member 元素从 source 集合移动到 destination 集合 |
| 10 | SPOP key
移除并返回集合中的一个随机元素 |
| 11 | SRANDMEMBER key [count]
返回集合中一个或多个随机数 |
| 12 | SREM key member1 [member2]
移除集合中一个或多个成员 |
| 13 | SUNION key1 [key2]
返回所有给定集合的并集 |
| 14 | SUNIONSTORE destination key1 [key2]
所有给定集合的并集存储在 destination 集合中 |
| 15 | SSCAN key cursor [MATCH pattern] [COUNT count]
迭代集合中的元素 |


I am surprised no one advised you to use either a Hash Table or a Sorted Set which combine advantages of allowing duplicity (by storing the number of elements as value - Hash Table, or score - Sorted Set) and indexing members by nature of a hash table/set.
Hash Table

To check for a key existence, use the HGETcommand. It returns a nil answer if the specified member does not exist.

To add a new member, simply use HINCRBY which will either update the value (ie the number of elements with the member name) or create a new member if it does not exist.
Sorted Set

To check for a key existence, use either one of the three following commands:

ZSCORE
ZRANK
ZREVRANK

They return a nil answer if the specified member does not exist.

To add a new member, simply use ZINCRBY which will either update the score (ie the number of elements with the member name) or create a new member if it does not exist.

To sum up: Sorted Sets or Hash Tables allow you to make all the operations with your requirements with a single command.

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 一、Redis基础 1.概述 Redis是一个开源,高级的键值存储和一个适用的解决方案,用于构建高性能,可扩展的W...
    郑元吉阅读 2,372评论 0 0
  • NOSQL类型简介键值对:会使用到一个哈希表,表中有一个特定的键和一个指针指向特定的数据,如redis,volde...
    MicoCube阅读 9,481评论 2 27
  • 源地址:http://blog.csdn.net/gjanuary/article/details/5207699...
    Freeasthew_a098阅读 5,397评论 0 6
  • 这篇文章主要介绍了30个php操作redis常用方法代码例子,本文其实不止30个方法,可以操作string类型、l...
    我是没头脑丶阅读 5,742评论 0 21
  • phpredis是php的一个扩展Redis::__construct构造函数$redis = new Redis...
    hello大象阅读 4,093评论 0 2