魏长东

weichangdong

正在查看: lua 分类下的文章(第 1 页 / 共 33 篇)

lua 验证码

copy fron 阿里云栖(记录备用)

 

安装gd,GD版本:gd-2.0.33

yum install -y libjpeg-devel libpng-devel freetype-devel fontconfig-devel libXpm-devel

$ wget http://www.boutell.com/gd/http/gd-2.0.33.tar.gz
$ tar zvxf gd-2.0.33.tar.gz
$ cd gd-2.0.33
$ ./configure
$ make && make install

»»阅读全文

lua xml reader

xmlreader

自我感觉和PHP的xmlreader很像。

Provides the XmlReader interface. All functions may return nil plus an error message if LibXML2 reports an error.

  • lifecycle functions
  • methods that move the reader
  • iterators
  • others
  • extract information related to current node
  • extract information related to this reader instance

reader:read()

Moves the position of reader to the next node in the stream. Returns true if the node was read successfully, false if there are no more nodes to be read.

reader:read_inner_xml()

Reads the contents of the current node's child nodes, does not include the tags of the current node. Returns a string containing the XML content, or nil plus an error message if the current node is neither an element nor attribute, or has no child nodes.

reader:read_outer_xml()

Reads the contents of the current node, including child nodes and markup. Returns a string containing the XML content, or nil plus an error message if the current node is neither an element nor attribute, or has no child nodes.

reader:read_string()

Reads the contents of an element of a text node as a string. Returns a string or nil plus an error message if the reader is position on any other type of node.

reader:read_attribute_value()

Parses an attribute value into one or more Text and EntityReference nodes. Returns true in case of success, false if the reader was not positioned on an attribute node or all the attribute value have been read.

»»阅读全文

Nginx 的 Echo 模块 —— echo-nginx-module

 location /hello {
    echo "hello, world!";
  }

  location /hello {
    echo -n "hello, "
    echo "world!";
  }

  location /timed_hello {
    echo_reset_timer;
    echo hello world;
    echo "'hello world' takes about $echo_timer_elapsed sec.";
    echo hiya igor;
    echo "'hiya igor' takes about $echo_timer_elapsed sec.";
  }

  location /echo_with_sleep {
    echo hello;
    echo_flush;  # ensure the client can see previous output immediately
    echo_sleep   2.5;  # in sec
    echo world;
  }

  # in the following example, accessing /echo yields
  #   hello
  #   world
  #   blah
  #   hiya
  #   igor
  location /echo {
      echo_before_body hello;
      echo_before_body world;
      proxy_pass $scheme://127.0.0.1:$server_port$request_uri/more;
      echo_after_body hiya;
      echo_after_body igor;
  }
  location /echo/more {
      echo blah;
  }

  # the output of /main might be
  #   hello
  #   world
  #   took 0.000 sec for total.
  # and the whole request would take about 2 sec to complete.
  location /main {
      echo_reset_timer;
 
      # subrequests in parallel
      echo_location_async /sub1;
      echo_location_async /sub2;
 
      echo "took $echo_timer_elapsed sec for total.";
  }
  location /sub1 {
      echo_sleep 2;
      echo hello;
  }
  location /sub2 {
      echo_sleep 1;
      echo world;
  }

  # the output of /main might be
  #   hello
  #   world
  #   took 3.003 sec for total.
  # and the whole request would take about 3 sec to complete.
  location /main {
      echo_reset_timer;
 
      # subrequests in series (chained by CPS)
      echo_location /sub1;
      echo_location /sub2;
 
      echo "took $echo_timer_elapsed sec for total.";
  }
  location /sub1 {
      echo_sleep 2;
      echo hello;
  }
  location /sub2 {
      echo_sleep 1;
      echo world;
  }

  # Accessing /dup gives
  #   ------ END ------
  location /dup {
    echo_duplicate 3 "--";
    echo_duplicate 1 " END ";
    echo_duplicate 3 "--";
    echo;
  }

  # /bighello will generate 1000,000,000 hello's.
  location /bighello {
    echo_duplicate 1000_000_000 'hello';
  }

  # echo back the client request
  location /echoback {
    echo_duplicate 1 $echo_client_request_headers;
    echo "\r";
 
    echo_read_request_body;
 
    echo_request_body;
  }

  # GET /multi will yields
  #   querystring: foo=Foo
  #   method: POST
  #   body: hi
  #   content length: 2
  #   ///
  #   querystring: bar=Bar
  #   method: PUT
  #   body: hello
  #   content length: 5
  #   ///
  location /multi {
      echo_subrequest_async POST '/sub' -q 'foo=Foo' -b 'hi';
      echo_subrequest_async PUT '/sub' -q 'bar=Bar' -b 'hello';
  }
  location /sub {
      echo "querystring: $query_string";
      echo "method: $echo_request_method";
      echo "body: $echo_request_body";
      echo "content length: $http_content_length";
      echo '///';
  }

  # GET /merge?/foo.js&/bar/blah.js&/yui/baz.js will merge the .js resources together
  location /merge {
      default_type 'text/javascript';
      echo_foreach_split '&' $query_string;
          echo "/* JS File $echo_it */";
          echo_location_async $echo_it;
          echo;
      echo_end;
  }

  # accessing /if?val=abc yields the "hit" output
  # while /if?val=bcd yields "miss":
  location ^~ /if {
      set $res miss;
      if ($arg_val ~* '^a') {
          set $res hit;
          echo $res;
      }
      echo $res;
  }

好的测试工具wrk

比ab好,我感觉,在并发数量上,wrk更加优胜。可以用lua脚本加强一些功能。

lua和php运行速度

在网上看到一篇文章,同样的功能用集中语言实现,然后执行比较速度。其中有我喜欢的php和正在学习的lua,就把代码搞到自己的机器上运行了,lua比php慢,但是luajit却快了十倍,让我开始喜欢luajit了。我喜欢一个语言,很简单,只需要一个简单的理由。

这是执行时间

php time:17811.112880707ms php5.6

lua time:31530ms lua5.14

luajit time:2790ms

检测你的nginx是否使用了jit

ldd /usr/local/openresty/nginx/sbin/nginx

下面上代码

»»阅读全文

lua的协程学习

有些知识当时看懂了,但是过一阵子发现看半天又看不懂了,真是不知所谓。

可能是学习没有复习的原因吧。

Lua所支持的协程全称被称作协同式多线程,协同程序与线程差不多,也就是一条执行序列,拥有自己独立的栈,局部变量和指令指针,同时又与其它协同程序共享全局变量和其它大部分东西。线程与协同程序的主要区别在于,一个具有多线程的程序可以同时运行几个线程,而协同程序却需要彼此协作地运行。就是说,一个具有多个协同程序的程序在任何时刻只能运行一个协同程序,并且正在运行的协同程序只会在其显示地挂起时,它的执行才会暂停。

我的理解。就是暂停程序,等下一次调用,然后从上次停下的地方继续执行。

»»阅读全文

Lua中的错误与异常

Lua中error的处理:Lua经常作为扩展语言嵌入在别的应用中,所以不能当错误发生时简单的崩溃或者退出。相反,当错误发生时Lua结束当前的chunk并返回到应用中。

 

  当Lua遇到不期望的情况时就会抛出错误,你也可以通过调用error函数显式地抛出错误。

 

  当函数遇到异常有两个基本的动作:返回错误代码或者抛出错误。选择哪一种方式,没有固定的规则,不过基本的原则是:对于程序逻辑上能够避免的异常,以抛出错误的方式处理之,否则返回错误代码。

»»阅读全文

lua setmetatable学习

 

local table1 = {
111
}

local table2 = {
111
}
local mt = {}
mt.__add = function(t1, t2)
    for _, item in ipairs(t2) do
        table.insert(t1, item)
    end
	return t1
end
setmetatable(table1,mt)
--setmetatable(table2,mt)

tSum = table2 + table1
--[[
for k, v in pairs(tSum) do
    print(v)
end
--]]


local old = {
	wcd = 'im wcd'
}

local new = {
	wcd1 = 'im new wcd',
	yy = 'im new yy'
}
setmetatable(old,{
__index = new
})

--print(old.wcd1)


local old_fun = {}
old_fun.wcd = 'wcd'
old_fun.fun = function()
	return 'im fun'
end

local new_old = {
	default = 'im default'
}

local new_fun = function()
	return 'im new fun'
end

setmetatable(old_fun,{
	__index = new_old,
	__call = new_fun
})

print(old_fun.default)

print(old_fun())

算是堆lua的setmatetable有了多一点的认识.

Mac下卸载编译安装的lua

本来可以brew install lua的,但是装的是5.2版本的,所以卸载了,自己安装了一个5.14的。

最后发现,卸载之后,luarocks也跟着没了,就想着重新 用brew安装。

下面是卸载方法,就是吧安装生成的文件都给删了去。

/usr/local/bin/lua
/usr/local/bin/luac
/usr/local/include/lua.h
/usr/local/include/lua.hpp
/usr/local/include/luaconf.h
/usr/local/include/lualib.h
/usr/local/lib/liblua.a
/usr/local/lib/lua
/usr/local/lib/lua/5.1
/usr/local/share/lua
/usr/local/share/lua/5.1
/usr/local/man/man1/lua.1
/usr/local/man/man1/luac.1