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

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # 自定義函數 調用回調函數,并把一個數組參數作為回調函數的參數 ~~~ local args = {...} or {} methodName(unpack(args, 1, table.maxn(args))) ~~~ # 使用場景 你要調用的函數名是未知的 函數參數類型和數目也是未知的 一般常用于在定時器處理邏輯之中 > 偽代碼 ~~~ addTask(endTime, callback, params) if os.time() >= endTime then callback(unpack(params, 1, table.maxn(params))) end ~~~ # 小試牛刀 ~~~ local function run(x, y) ngx.say('run', x, y) end local function attack(targetId) ngx.say('targetId', targetId) end local function doAction(method, ...) local args = {...} or {} method(unpack(args, 1, table.maxn(args))) end doAction(run, 1, 2) doAction(attack, 1111) ~~~ 我們再新建一個模塊 sample ~~~ local _M = {} function _M:hello(str) ngx.say('hello', str) end function _M.world(str) ngx.say('world', str) end return _M ~~~ 這個時候我們可以這樣調用,代碼接上文 因為sample模塊的方法聲明方式的不同 所以在調用時有些區別 主要是.和:的區別 [https://github.com/humbut/openresty-best-practices/blob/master/lua/dot_diff.md](https://github.com/humbut/openresty-best-practices/blob/master/lua/dot_diff.md) ~~~ local sample = require "sample" doAction(sample.hello, sample, ' 123') -- 相當于sample:hello('123') doAction(sample.world, ' 321') -- 相當于sample.world('321') ~~~ # 實戰演練 以下代碼為360公司公共組件之緩存模塊,正是利用了部分特性 ~~~ -- { -- key="...", cache key -- exp_time=0, default expire time -- exp_time_fail=3, success expire time -- exp_time_succ=60*30, failed expire time -- lock={...} lock opsts(resty.lock) -- } function get_data_with_cache( opts, fun, ... ) local ngx_dict_name = "cache_ngx" -- get from cache local cache_ngx = ngx.shared[ngx_dict_name] local values = cache_ngx:get(opts.key) if values then values = json_decode(values) return values.res, values.err end -- cache miss! local lock = lock:new(ngx_dict_name, opts.lock) local elapsed, err = lock:lock("lock_" .. opts.key) if not elapsed then return nil, string.format("get data with cache not found and sleep(%ss) not found again", opts.lock_wait_time) end -- someone might have already put the value into the cache -- so we check it here again: values = cache_ngx:get(opts.key) if values then lock:unlock() values = json_decode(values) return values.res, values.err end -- get data local exp_time = opts.exp_time or 0 -- default 0s mean forever local res, err = fun(...) if err then exp_time = opts.exp_time_fail or exp_time else exp_time = opts.exp_time_succ or exp_time end -- update the shm cache with the newly fetched value cache_ngx:set(opts.key, json_encode({res=res, err=err}), exp_time) lock:unlock() return res, err end ~~~
                  <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>

                              哎呀哎呀视频在线观看