Redis之 集合(Sets)

来源:互联网 发布:企业名录数据库 编辑:程序博客网 时间:2024/06/06 18:48
Redis Sets 是不重复且无序的字符串元素的集合,用哈希表来保持字符串的唯一性。
Redis 在每次调用时可能按照任意顺序返回元素,因为对于元素的顺序并没有规定。
Sets 适合用于表示对象间的关系。
一个简单的建模方式是,对每一个希望标记的对象使用 set。这个 set 包含和对象相关联的标签的 ID。

假设我们想要给新闻打上标签。 假设新闻 ID 1000 被打上了 1,2,5 和 77 四个标签,我们可以使用一个 set 把 tag ID 和新闻条目关联起来:


1、添加

        /// <summary>        /// setid集合中添加value值        /// 添加一个或多个指定的member元素到集合的 key中.指定的一个或者多个元素member 如果已经在集合key中存在则忽略.        /// 如果集合key 不存在,则新建集合key,并添加member元素到集合key中.        /// 原指令:SADD key member [member ...]        /// 时间复杂度:O(1)        /// 参考:http://www.redis.cn/commands/sadd.html        /// </summary>        public void Add(string setid, string value)        {            Redis.AddItemToSet(setid, value);        }        /// <summary>        /// setid集合中添加list集合        /// </summary>        public void Add(string setid, List<string> list)        {            Redis.AddRangeToSet(setid, list);        }

2、获取

        /// <summary>        /// 仅提供setid参数,那么随机返回setid集合中的一个元素.        /// 原指令:SRANDMEMBER key [count]        /// 时间复杂度:O(1)        /// 参考:http://www.redis.cn/commands/srandmember.html        /// </summary>        public string GetRandomItemFromSet(string setid)        {            return Redis.GetRandomItemFromSet(setid);        }        /// <summary>        /// 返回集合存储的key的基数 (集合元素的数量).        /// 原指令:SCARD key        /// 时间复杂度:O(1)        /// 参考:http://www.redis.cn/commands/scard.html        /// </summary>        public long GetCount(string setid)        {            return Redis.GetSetCount(setid);        }        /// <summary>        /// 获取所有setid集合的值        /// </summary>        public HashSet<string> GetAllItemsFromSet(string setid)        {            return Redis.GetAllItemsFromSet(setid);        }

3、删除

        /// <summary>        /// 随机删除setid集合中的一个值        /// </summary>        public string PopItemFromSet(string setid)        {            return Redis.PopItemFromSet(setid);        }        /// <summary>        /// 随机删除setid集合中指定数量的值        /// </summary>        /// <param name="setid"></param>        /// <param name="count"></param>        /// <returns></returns>        public List<string> PopItemsFromSet(string setid, int count)        {            List<string> lists = new List<string>();            for (int i= 0; i < count;i++)            {                lists.Add(Redis.PopItemFromSet(setid));            }            return lists;        }        /// <summary>        /// 删除setid集合中的value        /// 原指令:SREM key member [member ...]        /// 时间复杂度:O(N)        /// 参考:http://www.redis.cn/commands/srem.html        /// </summary>        public void RemoveItemFromSet(string setid, string value)        {            Redis.RemoveItemFromSet(setid, value);        }        /// <summary>        /// 删除key        /// 同HashAPI中的Remove        /// </summary>        /// <param name="key"></param>        /// <returns></returns>        public bool Remove(string key)        {            return Redis.Remove(key);        }        /// <summary>        /// 删除指定的Key        /// 同HashAPI中的RemoveAll        /// </summary>        /// <param name="keys"></param>        public void RemoveAll(IEnumerable<string> keys)        {            Redis.RemoveAll(keys);        }

4、求并集

        /// <summary>        /// 返回setids多个集合中的并集,返回hashset        /// 原指令:SUNION key [key ...]        /// 时间复杂度:O(N)        /// 参考:http://www.redis.cn/commands/sunion.html        /// </summary>        public HashSet<string> GetUnionFromSets(string[] setids)        {            return Redis.GetUnionFromSets(setids);        }        /// <summary>        /// setids多个集合中的并集,放入newSetId集合中,如果newSetId集合存在, 则会被重写.        /// 原指令:SUNIONSTORE destination key [key ...]        /// 时间复杂度:O(N)        /// 参考:http://www.redis.cn/commands/sunionstore.html        /// </summary>        public void StoreUnionFromSets(string newSetId, string[] setids)        {            Redis.StoreUnionFromSets(newSetId, setids);        }

5、求差集

        /// <summary>        /// 返回一个集合与给定集合的差集的元素.        /// 原指令:SUNIONSTORE destination key [key ...]        /// 时间复杂度:O(N)        /// 参考:http://www.redis.cn/commands/sdiff.html        /// </summary>        /// <param name="fromKey"></param>        /// <param name="keys"></param>        /// <returns></returns>        public HashSet<string> GetDifferencesFromSet(string fromSetId, string[] setIds)        {            return Redis.GetDifferencesFromSet(fromSetId, setIds);        }        /// <summary>        /// 把fromSetId集合中的数据与setIds集合中的数据对比,fromSetId集合中不存在setIds集合中,则把这些不存在的数据放入newSetId集合中,如果newSetId集合存在, 则会被重写.        /// 返回一个集合与给定集合的差集的元素.        /// 原指令:SDIFFSTORE destination key [key ...]        /// 时间复杂度:O(N)        /// 参考:http://www.redis.cn/commands/sdiffstore.html        /// </summary>        public void StoreDifferencesFromSet(string newSetId, string fromSetId, string[] setIds)        {            Redis.StoreDifferencesFromSet(newSetId, fromSetId, setIds);        }

6、求交集

        /// <summary>        /// 返回指定所有的集合的成员的交集.        /// 原指令:SINTER key[key...]        /// 时间复杂度:O(N*M)        /// 参考:http://www.redis.cn/commands/sdiffstore.html        /// </summary>        /// <param name="keys"></param>        /// <returns></returns>        public HashSet<string> GetIntersectFromSets(string[] setIds)        {            return Redis.GetIntersectFromSets(setIds);        }        /// <summary>        /// 返回指定所有的集合的成员的交集. 将结果保存在 newKey集合中,如果newKey集合存在, 则会被重写.        /// 原指令:SINTERSTORE destination key [key ...]        /// 时间复杂度:O(N*M)        /// 参考:http://www.redis.cn/commands/sinterstore.html        /// </summary>        /// <param name="newKey"></param>        /// <param name="keys"></param>        public void StoreIntersectFromSet(string newSetIds, string[] setIds)        {            Redis.StoreIntersectFromSets(newSetIds, setIds);        }

7、移动

        /// <summary>        /// 从fromSetId集合中移除值为value的值,并把value添加到toSetId集合中        /// 原指令:SMOVE source destination member        /// 时间复杂度:O(1)        /// 参考:http://www.redis.cn/commands/smove.html        /// </summary>        public void MoveBetweenSets(string fromSetId, string toSetId, string value)        {            Redis.MoveBetweenSets(fromSetId, toSetId, value);        }


原创粉丝点击