zoukankan      html  css  js  c++  java
  • nginx中的Lua

    Lua的搜索包的方法:

      就是把?替换成你需要加载的模块的相对路径,?可以理解为占位符。

      首次加载:local default_conf   =  require('plugins.config_default')

      再次加载:

        package.loaded['plugins.config_default']这个可以动态的添加表也可以添加一些lua静态文件的表

        require('plugins.config_default')这个是添加一些lua静态文件的表

    lua_package_path "$prefix/../lualib/?.lua;$prefix/luaext/?.lua;$prefix/luaext/vendor/?.lua;;";
    lua_package_cpath "$prefix/../lualib/?.so;;";

     定义一个Lua模块:

    local _M = {
    }
    
    --成员变量
    _M._VERSION = '1.0.0'
    _M._DESCRIPTION = 'test desc'
    
    --局部变量
    local msb_router=  require('core.router')
    local table_insert = table.insert
    local string_find = string.find
    local str_low = string.lower
    local ngx_var = ngx.var
    
    --成员函数
    function _M.route()    
    end
    
    --私有函数
    local function prepare_route()
    end
    
    return _M

    Lua的面向对象:

      self就是指冒号前面的东西,也就是指调用者自己。 

      当你通过键来访问 table 的时候,如果这个键没有值,那么Lua就会寻找该table的metatable(假定有metatable)中的__index 键。如果__index包含一个表格,Lua会在表格中查找相应的键。

    local Object = {}
    Object.__index = Object --为了让别人继承自己
    
    
    function Object:new()
    end
    
    
    function Object:extend()
      local cls = {} --创建新表
      for k, v in pairs(self) do --复制固有属性(__call,__index,__tostring)
        if k:find("__") == 1 then
          cls[k] = v
        end
      end
      cls.__index = cls --为了让别的类继承自己
      cls.super = self  --指定父类
      setmetatable(cls, self) --设定元表,落实继承关系
      return cls --返回创建的新表
    end
    
    
    function Object:implement(...)
      for _, cls in pairs({...}) do
        for k, v in pairs(cls) do
          if self[k] == nil and type(v) == "function" then
            self[k] = v
          end
        end
      end
    end
    
    
    function Object:is(T)
      local mt = getmetatable(self)
      while mt do
        if mt == T then
          return true
        end
        mt = getmetatable(mt)
      end
      return false
    end
    
    
    function Object:__tostring()
      return "Object"
    end
    
    
    function Object:__call(...)
      local obj = setmetatable({}, self)
      obj:new(...)
      return obj
    end
    
    
    return Object

      如果想要继承前面的类Object需要这样写:

    local Object = require "vendor.classic"
    local
    BasePlugin = Object:extend() --基于父类创建新表,这个时候extend函数中self就是Object function BasePlugin:new(name) --覆盖父类函数 self._name = name end function BasePlugin:access() --子类新加的函数 end function BasePlugin:header_filter() --子类新加的函数 end return BasePlugin

      如果想要继承BasePlugin需要这样写:

    local BasePlugin = require "plugins.base_plugin"
    local RedirectTransformerPluginHandler = BasePlugin:extend() --基于父类BasePlugin创建表,这个时候在extend函数中self的值就是BasePlugin
    
    function RedirectTransformerPluginHandler:new()
        RedirectTransformerPluginHandler.super.new(self, "redirect-transformer-plugin") --调用父类的同名函数
    end
    
    function RedirectTransformerPluginHandler:header_filter()
        RedirectTransformerPluginHandler.super.header_filter(self) --调用父类的同名函数
        
    end
    
    return RedirectTransformerPluginHandler

    Lua的缓冲:

      代码的缓冲:

        语法:lua_code_cache on | off

        默认:lua_lua_cache on

        上下文:http, server, location, location if;一般在server之外

      

    Lua的库函数:

      Lua中的库函数,都是可以直接使用的,不需要require

      Lua的table库函数insert、remove、concat、sort详细介绍

      Lua中的string库

    nginx+lua的命令:

      init_by_lua后面只能跟字符串

      init_by_lua_block后面是用{}抱起来的Lua语句

      init_by_lua_file后面加Lua文件

      init_by_lua:在nginx启动时执行,位置在server之外

      init_worker_by_lua:在worker进程启动时执行,位置在server之外

      init_by_lua和init_worker_by_lua中定义的变量为全局变量

    参考文献:

    https://www.cnblogs.com/jimodetiantang/p/9257819.html

    Lua参考手册:https://www.codingnow.com/2000/download/lua_manual.html

  • 相关阅读:
    WindowsServer 2016激活
    selenium浏览器复用与原理分析
    react脚手架: 配置代理
    网络请求方式
    Java: Excel导入导出
    react 组件通信方式
    react: reactrouterdom
    react: 高阶函数及函数柯里化
    react: 事件处理
    react: 生命周期
  • 原文地址:https://www.cnblogs.com/erdanyang/p/10801732.html
Copyright © 2011-2022 走看看