lua連接redis集群


連接redis集群需要用到llua-resty-redis-cluster模塊

github地址:https://github.com/cuiweixie/lua-resty-redis-cluster

下載完成后,只需要用到包中2個文件rediscluster.luaredis_slot.c

.c文件無法在nginx配置文件中引入,需要編譯成.so文件,編譯命令:  gcc SOURCE_FILES -fPIC -shared -o TARGET

如下則是連接redis集群代碼:

local config = {
                name = "test",
                serv_list = {
                    {ip="127.0.0.1", port = 3100},
                    {ip="127.0.0.1", port = 3101},
                    {ip="127.0.0.1", port = 3102},
                    {ip="127.0.0.1", port = 3200},
                    {ip="127.0.0.1", port = 3201},
                    {ip="127.0.0.1", port = 3202},
                },
            }
            local redis_cluster = require "resty.rediscluster"
            local red = redis_cluster:new(config)
            for i = 1, 2 do
                red:init_pipeline()
                red:set("dog", "an animal")
                red:get("dog")
                red:set("dog", "hello")
                red:get("dog")
                local results = red:commit_pipeline()
                local cjson = require "cjson"
                ngx.say(cjson.encode(results))
            end
            red:close()

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM