<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>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                Redis協程網絡庫 lua-resty-redis ## 簡介 lua-resty-redis 是由著名OpenResty社區成員Agent Zhang(章亦春)創建的。這是一個與OpenResty集成的Lua模塊,允許你在Nginx環境中直接進行Redis操作。利用OpenResty的強大功能,lua-resty-redis提供了異步非阻塞的Redis API,幫助開發者構建高性能、高并發的應用。 lua-resty-redis的核心在于其非阻塞I/O模型。它基于OpenResty的ngx.socket.tcp()接口實現,利用了LuaJIT的高效性能和Nginx事件循環機制。這意味著在處理大量并發請求時,即使Redis服務器繁忙,也不會導致Nginx的工作線程被阻塞,從而提高了整體系統的響應速度。 此外,該庫提供了豐富的Redis命令支持,包括但不限于數據讀寫、哈希操作、集合操作、有序集合、發布訂閱等。它的API清晰簡潔,易于理解和使用,使得你可以快速地將Redis功能融入到你的OpenResty應用程序中。 ## 應用場景 * 緩存管理:利用Redis的高速讀寫能力,可以作為動態內容的高速緩存層,降低對后端數據庫的壓力。 * 分布式會話:借助lua-resty-redis,可以在多臺服務器之間共享用戶的會話狀態。 * 消息隊列:通過其發布的訂閱功能,可以構建簡單的消息隊列系統,實現任務的異步處理。 * 實時數據分析:利用Redis的數據結構,如計數器、集合等,進行實時統計和分析。 ## 主要特點 * 非阻塞I/O - 基于OpenResty的異步TCP套接字接口,確保高并發場景下的效率。 * Redis命令支持 - 提供了Redis的所有主要命令,方便你執行各種操作。 * 錯誤處理 - 提供了良好的錯誤處理機制,便于定位和解決問題。 * 可擴展性 - 可以自定義連接池策略,適應不同規模和需求的系統。 * 簡潔API - 易于理解和使用的API,加速開發過程。 ## 安裝 這里通過OPM工具包安裝,更多請查看[OpenResty實戰系列 | 包管理工具OPM和LuaRocks](https://mp.weixin.qq.com/s/s6-fukvBWdMuYRDa-cYZHw) ```lua opm get openresty/lua-resty-redis ``` 版本信息 ```lua # opm info openresty/lua-resty-redis Name : lua-resty-redis Version : 0.27 Abstract : Lua redis client driver for the ngx_lua based on the cosocket API Author : Yichun "agentzh" Zhang (agentzh) Account : openresty Code Repo : https://github.com/openresty/lua-resty-redis License : BSD 2-Clause "Simplified" or "FreeBSD" license Original Work : yes ``` ## 基礎使用 使用 lua-resty-redis 設置和獲取一個鍵值對基本示例 `redis_test_01.lua`文件 ```lua --[[----------------------------------------------------------------------- * | Copyright (C) Shaobo Wan (Tinywan) * |------------------------------------------------------------------------ --]] -- redis config local redis = require "resty.redis" local red = redis:new() red:set_timeouts(1000, 1000, 1000) -- 1 秒 -- 通過宿主機鏈接 local ok, err = red:connect("192.168.13.168", 6379) if not ok then ngx.say("[x] failed to connect: ", err) return end -- 設置權限 local res, err = red:auth("123456") if not res then ngx.say("[x] failed to authenticate: ", err) return end -- 設置權限 -- 請注意這里 auth 的調用過程 local count, err = red:get_reused_times() ngx.say("[x] get_reused_times count: ", count) if 0 == count then res, err = red:auth("123456") if not res then ngx.say("[x]failed to authenticate: ", err) return end elseif err then ngx.say("[x]failed to get reused times: ", err) return end ngx.say("[x] set result: ", ok) local res, err = red:get("name") if not res then ngx.say("[x] failed to get name: ", err) return end if res == ngx.null then ngx.say("[x] name not found.") return end ngx.say("[x] get name : ", res) -- 連接池大小是100個,并且設置最大的空閑時間是 10 秒 local ok, err = red:set_keepalive(10000, 100) if not ok then ngx.say("[x] failed to set keepalive: ", err) return end ``` > **`red:get_reused_times()`方法** * 如果當前連接不是從內建連接池中獲取的,該方法總是返回 `0` ,也就是說,該連接還沒有被使用過。 * 如果連接來自連接池,那么返回值永遠都是非零。 這個方法可以用來確認當前連接是否來自池子。對于 Redis 授權,實際上只需要建立連接后,首次認證一下,后面只需直接使用即可。換句話說,從連接池中獲取的連接都是經過授權認證的,只有新創建的連接才需要進行授權認證。所以大家就看到了 `count, err = red:get\_reused\_times()` 這段代碼,并有了下面 `if 0 == count then` 的判斷邏輯。 > 通過curl腳本測試請求打印結果 ```lua $ curl -i http://openresty.tinywan.com/lua_redis_test HTTP/1.1 200 OK Server: openresty/1.17.8.2 Date: Tue, 23 Jul 2024 07:16:23 GMT Content-Type: text/html; charset=utf-8 Transfer-Encoding: chunked Connection: keep-alive Vary: Accept-Encoding [x] set result: OK [x] get name : Tinywan ``` 如果指定的密碼是錯誤的,那么上面的示例將向HTTP客戶端輸出以下內容: ``` failed to authenticate: ERR invalid password ``` ## 事務支持 這個庫支持Redis事務 `redis_transactions_test.lua` 文件 ```lua --[[----------------------------------------------------------------------- * | Copyright (C) Shaobo Wan (Tinywan) * |------------------------------------------------------------------------ --]] local cjson = require "cjson" -- redis config local redis = require "resty.redis" local red = redis:new() red:set_timeouts(1000, 1000, 1000) -- 1 秒 -- 通過宿主機鏈接 local ok, err = red:connect("192.168.13.168", 6379) if not ok then ngx.say("[x] failed to connect: ", err) return end -- 設置權限 local res, err = red:auth("123456") if not res then ngx.say("[x] failed to authenticate: ", err) return end -- 設置權限 local ok, err = red:multi() if not ok then ngx.say("failed to run multi: ", err) return end ngx.say("[x] multi ans: ", cjson.encode(ok)) local ans, err = red:set("resty_name", "Tinywan") if not ans then ngx.say("[x] failed to run sort: ", err) return end ngx.say("[x] set ans: ", cjson.encode(ans)) local ans, err = red:lpop("resty_name") if not ans then ngx.say("[x] failed to run sort: ", err) return end ngx.say("[x] set ans: ", cjson.encode(ans)) ans, err = red:exec() ngx.say("[x] exec ans: ", cjson.encode(ans)) red:close() ``` 通過curl腳本測試請求打印結果 ```lua $ curl -i http://openresty.tinywan.com/lua_redis_transactions_test HTTP/1.1 200 OK Server: openresty/1.17.8.2 Date: Tue, 23 Jul 2024 07:29:53 GMT Content-Type: text/html; charset=utf-8 Transfer-Encoding: chunked Connection: keep-alive Vary: Accept-Encoding [x] multi ans: "OK" [x] set ans: "QUEUED" [x] set ans: "QUEUED" [x] exec ans: ["OK",[false,"WRONGTYPE Operation against a key holding the wrong kind of value"]] ``` ## 小結 lua-resty-redis是一個強大的工具,能夠幫助開發者充分利用OpenResty和Redis的優勢,構建高性能的Web服務。無論是簡單的緩存解決方案還是復雜的分布式系統,它都能提供穩定且高效的支撐。如果你正在尋找一個在OpenResty環境中操作Redis的解決方案,那么lua-resty-redis絕對值得一試!
                  <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>

                              哎呀哎呀视频在线观看