设为首页 收藏本站
查看: 3484|回复: 0

[经验分享] 使用redis构造优先级队列

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-7-19 13:56:11 | 显示全部楼层 |阅读模式
  redis作者@antirez在其blogHow to take advantage of Redis just adding it to your stack中提到:“Similarly using sorted sets it is possible to implement priority queues easily.”。
  本文将会探讨下如何使用redis提供的sorted sets数据结构,构造高效率的优先级队列。

什么是sorted sets
  以下段落来自reids.io->Data types。
  Redis Sorted Sets are, similarly to Redis Sets, non repeating collections of Strings. The difference is that every member of a Sorted Set is associated with score, that is used in order to take the sorted set ordered, from the smallest to the greatest score. While members are unique, scores may be repeated.
  With sorted sets you can add, remove, or update elements in a very fast way (in a time proportional to the logarithm of the number of elements). Since elements are taken in order and not ordered afterwards, you can also get ranges by score or by rank (position) in a very fast way. Accessing the middle of a sorted set is also very fast, so you can use Sorted Sets as a smart list of non repeating elements where you can quickly access everything you need: elements in order, fast existence test, fast access to elements in the middle!
  In short with sorted sets you can do a lot of tasks with great performance that are really hard to model in other kind of databases.
  With Sorted Sets you can:


  • Take a leader board in a massive online game, where every time a new score is submitted you update it using ZADD. You can easily take the top users using ZRANGE, you can also, given an user name, return its rank in the listing using ZRANK. Using ZRANK and ZRANGE together you can show users with a score similar to a given user. All very quickly.
  • Sorted Sets are often used in order to index data that is stored inside Redis. For instance if you have many hashes representing users, you can use a sorted set with elements having the age of the user as the score and the ID of the user as the value. So using ZRANGEBYSCORE it will be trivial and fast to retrieve all the users with a given interval of ages.
  • Sorted Sets are probably the most advanced Redis data types, so take some time to check the full list of Sorted Set commands to discover what you can do with Redis!

使用sorted sets构造优先级队列
  sorted sets有如下三个命令:
  1.ZADD key score member [score] [member]
  以O(log(N))的复杂度,向集合中加入一个元素。如下所示:



redis 127.0.0.1:6379> ZADD "www.baidu.com" 1 "first_page" 2 "second_page" 3 "third_page" 3 "another_page"
(integer) 4
  2.ZREVRANGE key start stop [WITHSCORES]
  以O(log(N)+M)的复杂度,取元素。N是集合中元素个数,M是返回值的元素个数。使用WITHSCORES,将会同时返回对应元素的SCORE。在优先级队列中,我们只取最高优先级的一个元素,如下所示:



redis 127.0.0.1:6379> ZREVRANGE "www.baidu.com" 0 0
1) "third_page"
redis 127.0.0.1:6379> ZREVRANGE "www.baidu.com" 0 0 WITHSCORES
1) "third_page"
2) "3"
  3.ZREM key member [member]
  以O(log(N))的复杂度,删除sorted set中的特定元素。这里的member为ZREVRANGE中的返回值即可,如下所示:



redis 127.0.0.1:6379> ZREM "www.baidu.com" "third_page"
(integer) 1
  删除元素前后,优先级队列中的元素对比如下:



redis 127.0.0.1:6379> ZREVRANGE "www.baidu.com" 0 -1 WITHSCORES
1) "third_page"
2) "3"
3) "another_page"
4) "3"
5) "second_page"
6) "2"
7) "first_page"
8) "1"
redis 127.0.0.1:6379> ZREVRANGE "www.baidu.com" 0 -1 WITHSCORES
1) "another_page"
2) "3"
3) "second_page"
4) "2"
5) "first_page"
6) "1"
  据此,一个高效(O(logN)的复杂度)的优先级队列就可以使用了。

事件通知模式
  参照上述方法构造的优先级队列是非阻塞模式的,这样,如果当前Sorted Sets为空,要求调用方不断轮循(polling),这对使用者来说是非常不方便的。redis并未提供阻塞版本的ZREVRANGE,但是使用blpop命令,可以实现优先级队列的阻塞语义。
  参考:Pattern: Event notification
  消费者(consumer)如下:



LOOP forever
WHILE ZREVRANGE(key,0,0) returns elements
... process elements ...
ZREM(key, elements)
END
BRPOP helper_key
END
  生产者(producer)如下:



MULTI
ZADD key element
LPUSH helper_key x
EXEC
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-88326-1-1.html 上篇帖子: 对ServiceStack.Redis的连接池进行故障转移改造 下篇帖子: Redis协议详解
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表