又要搬家咯

7.2號就得搬家了,東西還沒收拾,等到了週末再說吧,反正也就只剩下一天了。

今天晚上不知道咋了,很興奮。一個箭步躥到沙發上,爪子抓了大姐的頭,結果被逮住一頓“毒打”,打完之後就這樣了。思考貓生,誰都不讓摸。

排面

lua是個好東西,今天看老錢關於lua腳本在Redis中可以被原子性執行的介紹後,特意去看了看文檔。

Redis uses the same Lua interpreter to run all the commands. Also Redis guarantees that a script is executed in an atomic way: no other script or Redis command will be executed while a script is being executed. This semantic is similar to the one of MULTI / EXEC. From the point of view of all the other clients the effects of a script are either still not visible or already completed.

所以lua腳本被Redis內嵌的lua引擎執行的時候,不會出現併發問題,也就保證了lua腳本本身作爲原子性執行的環境。聯想到之前我設計的那個紅包Timeline清理,突然想到了不錯的拓展方法。可以避免現在爲了達到一個事件有且僅能被執行一次而設計的單個消費者模型。如果藉助lua腳本,內部封裝zrevrange+zrem。搞成類似zpop這種的,就可以同時運行多個消費者,這樣即便是今後用戶多了,紅包清理也不會受到影響。

<?php
$redis = new Redis();
$redis->pconnect("localhost", 44444);

$luascript = <<<SCRIPT
    local item = '';
    item = redis.call("zrevrange", KEYS[1], ARGV[1], ARGV[2], ARGV[3])
    redis.call("zrem", KEYS[1], item[1])
    return item[2]
SCRIPT;
$ret = $redis->eval($luascript, ["zset", "1", "1", "withscores"], 1);
var_dump($ret);

哈哈,還真的蠻好使。

從這個角度看LUA在這塊潛力很大誒,有個念頭,找時間好好學學這個腳本語言。回頭可以的話,對公司現有的phpredis包裝一下,lua腳本,就可以應對更多的場景了。。

此屬於更上層的行爲,交給底層redis-server去執行,所以移植性肯定也還行。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章