摘要:在實(shí)現(xiàn)延遲隊(duì)列時(shí)使用了腳本保證不同隊(duì)列間操作的原子性在中主要是通過(guò)個(gè)腳本方法保證不同隊(duì)列操作的原子性的一統(tǒng)計(jì)隊(duì)列任務(wù)數(shù)量方法統(tǒng)計(jì)隊(duì)列數(shù)量統(tǒng)計(jì)隊(duì)列數(shù)據(jù)量二隊(duì)列任務(wù)放入保留隊(duì)列三將任務(wù)由添加隊(duì)列到隊(duì)列中四將隊(duì)
Laravel在實(shí)現(xiàn)Redis延遲隊(duì)列時(shí)使用了Lua腳本保證不同隊(duì)列間操作的原子性
在Laravel5.1中主要是通過(guò)4個(gè)Lua腳本方法保證不同隊(duì)列操作的原子性的
一、統(tǒng)計(jì)隊(duì)列任務(wù)數(shù)量方法
1.llen 統(tǒng)計(jì)list隊(duì)列數(shù)量
2.zcard統(tǒng)計(jì)zset隊(duì)列數(shù)據(jù)量
/** * Get the Lua script for computing the size of queue. * * KEYS[1] - The name of the primary queue * KEYS[2] - The name of the "delayed" queue * KEYS[3] - The name of the "reserved" queue * * @return string */ public static function size() { return <<<"LUA" return redis.call("llen", KEYS[1]) + redis.call("zcard", KEYS[2]) + redis.call("zcard", KEYS[3]) LUA; }
二、pop隊(duì)列任務(wù)放入reserved(保留)隊(duì)列
/** * Get the Lua script for popping the next job off of the queue. * * KEYS[1] - The queue to pop jobs from, for example: queues:foo * KEYS[2] - The queue to place reserved jobs on, for example: queues:foo:reserved * ARGV[1] - The time at which the reserved job will expire * * @return string */ public static function pop() { return <<<"LUA" -- Pop the first job off of the queue... local job = redis.call("lpop", KEYS[1]) local reserved = false if(job ~= false) then -- Increment the attempt count and place job on the reserved queue... reserved = cjson.decode(job) reserved["attempts"] = reserved["attempts"] + 1 reserved = cjson.encode(reserved) redis.call("zadd", KEYS[2], ARGV[1], reserved) end return {job, reserved} LUA; }
三、將任務(wù)由添加reserved隊(duì)列到delayed隊(duì)列中
/** * Get the Lua script for releasing reserved jobs. * * KEYS[1] - The "delayed" queue we release jobs onto, for example: queues:foo:delayed * KEYS[2] - The queue the jobs are currently on, for example: queues:foo:reserved * ARGV[1] - The raw payload of the job to add to the "delayed" queue * ARGV[2] - The UNIX timestamp at which the job should become available * * @return string */ public static function release() { return <<<"LUA" -- Remove the job from the current queue... redis.call("zrem", KEYS[2], ARGV[1]) -- Add the job onto the "delayed" queue... redis.call("zadd", KEYS[1], ARGV[2], ARGV[1]) return true LUA; }
四、將reserved隊(duì)列滿足時(shí)間的任務(wù)合并到執(zhí)行隊(duì)列中
/** * Get the Lua script to migrate expired jobs back onto the queue. * * KEYS[1] - The queue we are removing jobs from, for example: queues:foo:reserved * KEYS[2] - The queue we are moving jobs to, for example: queues:foo * ARGV[1] - The current UNIX timestamp * * @return string */ public static function migrateExpiredJobs() { return <<<"LUA" -- Get all of the jobs with an expired "score"... local val = redis.call("zrangebyscore", KEYS[1], "-inf", ARGV[1]) -- If we have values in the array, we will remove them from the first queue -- and add them onto the destination queue in chunks of 100, which moves -- all of the appropriate jobs onto the destination queue very safely. if(next(val) ~= nil) then redis.call("zremrangebyrank", KEYS[1], 0, #val - 1) for i = 1, #val, 100 do redis.call("rpush", KEYS[2], unpack(val, i, math.min(i+99, #val))) end end return val LUA; }
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://specialneedsforspecialkids.com/yun/28594.html
摘要:年月日參考鏈接使用不得不明白的知識(shí)隊(duì)列文檔中文文檔本文環(huán)境隊(duì)列為什么使用隊(duì)列使用隊(duì)列的目的一般是異步執(zhí)行出錯(cuò)重試解釋一下異步執(zhí)行部分代碼執(zhí)行很耗時(shí)為了提高響應(yīng)速度及避免占用過(guò)多連接資源可以將這部分代碼放到隊(duì)列中異步執(zhí)行網(wǎng)站新用戶注冊(cè)后需要 Last-Modified: 2019年5月10日15:04:22 參考鏈接 使用 Laravel Queue 不得不明白的知識(shí) Laravel ...
摘要:大家有好的文章可以在評(píng)論下面分享出來(lái)共同進(jìn)步本文鏈接數(shù)組使用之道程序員進(jìn)階學(xué)習(xí)書(shū)籍參考指南教你在不使用框架的情況下也能寫(xiě)出現(xiàn)代化代碼巧用數(shù)組函數(shù)框架中間件實(shí)現(xiàn)沒(méi)錯(cuò),這就是面向?qū)ο缶幊淘O(shè)計(jì)模式需要遵循的個(gè)基本原則令人困惑的在中使用協(xié)程實(shí)現(xiàn)多任 大家有好的文章,可以在評(píng)論下面分享出來(lái), 共同進(jìn)步! 本文github鏈接 php PHP 數(shù)組使用之道 PHP程序員進(jìn)階學(xué)習(xí)書(shū)籍參考指南 教你...
摘要:把因執(zhí)行超時(shí)的隊(duì)列從集合重新到當(dāng)前執(zhí)行的隊(duì)列中。從要執(zhí)行的隊(duì)列中取任務(wù)可以看到在取要執(zhí)行的隊(duì)列的時(shí)候,同時(shí)會(huì)放一份到一個(gè)有序集合中,并使用過(guò)期時(shí)間戳作為分值。 (原文鏈接:https://blog.tanteng.me/2017/...) 在 Laravel 中使用 Redis 處理隊(duì)列任務(wù),框架提供的功能非常強(qiáng)大,但是最近遇到一個(gè)問(wèn)題,就是發(fā)現(xiàn)一個(gè)任務(wù)被多次執(zhí)行,這是為什么呢? 先說(shuō)...
前言 在若干次前的一場(chǎng)面試,面試官看我做過(guò)python爬蟲(chóng)/后端 的工作,順帶問(wèn)了我些后端相關(guān)的問(wèn)題:你覺(jué)得什么是后端? 送命題。當(dāng)時(shí)腦瓦特了,答曰:邏輯處理和數(shù)據(jù)增刪改查。。。 showImg(https://user-gold-cdn.xitu.io/2019/4/24/16a4ed4fc8c18078); 當(dāng)場(chǎng)被懟得體無(wú)完膚,羞愧難當(dāng)。事后再反思這問(wèn)題,結(jié)合資料總結(jié)了一下。發(fā)現(xiàn)自己學(xué)過(guò)的Re...
閱讀 2675·2023-04-25 15:15
閱讀 1316·2021-11-25 09:43
閱讀 1604·2021-11-23 09:51
閱讀 1079·2021-11-12 10:36
閱讀 2880·2021-11-11 16:55
閱讀 955·2021-11-08 13:18
閱讀 723·2021-10-28 09:31
閱讀 2048·2019-08-30 15:47