【问题标题】:Extend lua-resty-* modules and call parent function扩展 lua-resty-* 模块并调用父函数
【发布时间】:2015-07-31 22:23:12
【问题描述】:

我正在尝试为每个lua-resty-redislua-resty-memcachedlua-resty-mysql 模块编写一个扩展默认模块的小类。在我的子类中,我想从父类调用一个函数,但无论我阅读过什么 Lua 继承文档,都找不到合适的方法。

例如,我想覆盖connect() 函数,做一些事情并在某个时候调用父级的connect() 函数。但是怎么做呢?

local redis = require "resty.redis"


function redis.connect(self, ...)


  -- Do some stuff here


  local ok, err = parent:connect(...)


  -- Do some other stuff here


  return ok, err
end

如何做到这一点?

请注意,上述所有模块的结构如下:

local _M = { _VERSION = "0.1" }

local mt = { __index = _M }

function _M.new(self)
    return setmetatable({ foo = "bar" }, mt)
end

function _M.connect(self, ...)
    -- Connect
end

return _M

提前谢谢你!

【问题讨论】:

    标签: lua openresty


    【解决方案1】:
    local redis = require "resty.redis"
    local original_connect = redis.connect
    
    function redis.connect(self, ...)
    
    
      -- Do some stuff here
    
    
      local ok, err = original_connect(self, ...)
    
    
      -- Do some other stuff here
    
    
      return ok, err
    end
    

    【讨论】:

      猜你喜欢
      • 2017-07-31
      • 1970-01-01
      • 2012-02-11
      • 2021-01-30
      • 2016-05-28
      • 2015-09-28
      • 2013-11-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多