<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # Redis客戶端 lua-resty-redis是為基于cosocket API的ngx_lua提供的Lua redis客戶端,通過它可以完成Redis的操作。默認安裝OpenResty時已經自帶了該模塊,使用文檔可參考[https://github.com/openresty/lua-resty-redis](https://github.com/openresty/lua-resty-redis)。 ## 基本操作 #### 1. 創建redis/test_redis_baisc.lua ``` local function close_redes( red ) if not red then return end local ok, err = red:close() if not ok then ngx.say("close redis error:", err) end end local redis = require("resty.redis") -- 創建實例 local red = redis:new() -- 設置超時(毫秒) red:set_timeout(2000) -- 建立連接 local ip = "172.19.73.87" local port = 6379 local ok, err = red:connect(ip, port) if not ok then return end local res, err = red:auth("wsy@123456") if not res then ngx.say("connect to redis error : ", err) return end -- 調用API進行處理 res, err = red:set("msg", "hello world") if not res then ngx.say("set msg error : ", err) return close_redes(red) end -- 調用API獲取數據 local resp, err = red:get("msg") if not resp then ngx.say("get msg erro:", err) return close_redes(red) end -- 得到數據為空處理 if resp == ngx.null then resp = '' -- 比如默認值 end ngx.say("msg:", resp) close_redes(red) ``` 基本邏輯很簡單,要注意此處判斷是否為nil,需要跟ngx.null比較。 #### 2. openResty.conf配置文件 ``` location /lua_redis_basic { default_type 'text/html'; lua_code_cache on; content_by_lua_file /usr/openResty/lua/redis/test_redis_basic.lua; } ``` 訪問如http://127.0.0.1/lua_redis_basic進行測試,正常情況得到如下信息 msg : hello world #### 3. 連接池 建立TCP連接需要三次握手而釋放TCP連接需要四次握手,而這些往返時延僅需要一次,以后應該復用TCP連接,此時就可以考慮使用連接池,即連接池可以復用連接。 我們只需要將之前的close_redis函數改造為如下即可: ``` local function close_redes( red ) if not red then return end -- 釋放連接(連接池實現) local pool_max_idle_time = 10000 -- 毫秒 local pool_size = 100 --連接池大小 local ok, err = red:set_keepalive(pool_max_idle_time, pool_size) if not ok then ngx.say("set keepalive error : ", err) end end ``` 即設置空閑連接超時時間防止連接一直占用不釋放;設置連接池大小來復用連接。 <br/> 此處假設調用red:set_keepalive(),連接池大小通過nginx.conf中http部分的如下指令定義: **默認連接池大小,默認30** lua_socket_pool_size 30; **默認超時時間,默認60s** lua_socket_keepalive_timeout 60s; <br/> **注意:** 1. 連接池是每Worker進程的,而不是每Server的; 2. 當連接超過最大連接池大小時,會按照LRU算法回收空閑連接為新連接使用; 3. 連接池中的空閑連接出現異常時會自動被移除; 4. 連接池是通過ip和port標識的,即相同的ip和port會使用同一個連接池(即使是不同類型的客戶端如Redis、Memcached); 5. 連接池第一次set_keepalive時連接池大小就確定下了,不會再變更; 6. cosocket的連接池[http://wiki.nginx.org/HttpLuaModule#tcpsock:setkeepalive](http://wiki.nginx.org/HttpLuaModule#tcpsock:setkeepalive)。 #### 4. pipeline pipeline即管道,可以理解為把多個命令打包然后一起發送;MTU(Maxitum Transmission Unit 最大傳輸單元)為二層包大小,一般為1500字節;而MSS(Maximum Segment Size 最大報文分段大小)為四層包大小,其一般是1500-20(IP報頭)-20(TCP報頭)=1460字節;因此假設我們執行的多個Redis命令能在一個報文中傳輸的話,可以減少網絡往返來提高速度。因此可以根據實際情況來選擇走pipeline模式將多個命令打包到一個報文發送然后接受響應,而Redis協議也能很簡單的識別和解決粘包。 修改之前的代碼段 test_pipeline.lua ``` -- local function close_redes( red ) -- if not red then -- return -- end -- local ok, err = red:close() -- if not ok then -- ngx.say("close redis error:", err) -- end -- end local function close_redes( red ) if not red then return end -- 釋放連接(連接池實現) local pool_max_idle_time = 10000 -- 毫秒 local pool_size = 100 --連接池大小 local ok, err = red:set_keepalive(pool_max_idle_time, pool_size) if not ok then ngx.say("set keepalive error : ", err) end end local redis = require("resty.redis") -- 創建實例 local red = redis:new() -- 設置超時(毫秒) red:set_timeout(2000) -- 建立連接 local ip = "172.19.73.87" local port = 6379 local ok, err = red:connect(ip, port) if not ok then return end local res, err = red:auth("wsy@123456") if not res then ngx.say("connect to redis error : ", err) return end red:init_pipeline() red:set("msg1", "hello1") red:set("msg2", "hello2") red:get("msg1") red:get("msg2") local respTable, err = red:commit_pipeline() -- 得到數據為空處理 if respTable == ngx.null then respTable = {} end -- 結果是按照執行順序返回的一個table for i, v in ipairs(respTable) do ngx.say("msg : ", v, "<br/>") end close_redes(red) ``` 通過init_pipeline()初始化,然后通過commit_pipieline()打包提交init_pipeline()之后的Redis命令;返回結果是一個lua table,可以通過ipairs循環獲取結果; 配置相應location,測試得到的結果 openResty.conf配置文件 ``` location /lua_redis_pipeline { default_type 'text/html'; lua_code_cache on; content_by_lua_file /usr/openResty/lua/redis/test_pipeline.lua; } ``` msg : OK msg : OK msg : hello1 msg : hello2 <br/> Redis Lua腳本 利用Redis單線程特性,可以通過在Redis中執行Lua腳本實現一些原子操作。如之前的red:get("msg")可以通過如下兩種方式實現: 直接eval: ``` local?resp,?err?=?red:eval("return?redis.call('get',?KEYS[1])",?1,?"msg"); ``` script load然后evalsha??SHA1 校驗和,這樣可以節省腳本本身的服務器帶寬: ``` local?sha1,?err?=?red:script("load",??"return?redis.call('get',?KEYS[1])");?? if?not?sha1?then?? ???ngx.say("load?script?error?:?",?err)?? return?close_redis(red)?? end?? ngx.say("sha1?:?",?sha1,?"")?? local?resp,?err?=?red:evalsha(sha1,?1,?"msg"); ``` 首先通過script load導入腳本并得到一個sha1校驗和(僅需第一次導入即可),然后通過evalsha執行sha1校驗和即可,這樣如果腳本很長通過這種方式可以減少帶寬的消耗。 此處僅介紹了最簡單的redis lua腳本,更復雜的請參考官方文檔學習使用。 另外Redis集群分片算法該客戶端沒有提供需要自己實現,當然可以考慮直接使用類似于Twemproxy這種中間件實現。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看