某些时候用python实现的接口性能不够强劲,需要换种方法来提高性能,这不就用lua试试。
常见的redis lua的组合功能 有接口防止过度访问,动态的数据加载,接口的数据缓存
redis没有直接提供一个http的接口,性能的比较的话,lua的能力要比php、python要强的不少。。。
网上有很多的性能的比较,我也做过几次的压力测试,lua的性能确实很强。。。。
废话,首先安装nginx,然后加入lua环境。
git clone https://github.com/simpl/ngx_devel_kit.git git clone https://github.com/chaoslawful/lua-nginx-module.git git clone https://github.com/agentzh/redis2-nginx-module.git git clone https://github.com/agentzh/set-misc-nginx-module.git git clone https://github.com/agentzh/echo-nginx-module.git yum -y install pcre pcre-dev* wget http://nginx.org/download/nginx-1.3.14.tar.gz tar zxvf nginx-1.3.14.tar.gz cd nginx-1.3.14 ./configure --prefix=/usr/local/nginx --add-module=../ngx_devel_kit/ --add-module=../lua-nginx-module --add-module=../redis2-nginx-module --add-module=../set-misc-nginx-module --add-module=../echo-nginx-module make && make install
注:在 server 段里,加入代码,如果不加此代码或者设置为 on 时,则需要重启 Nginx。
lua_code_cache off;
下面是 Nginx.conf 的配置
server{ listen 80; server_name test.lua.com; #http://test.lua.com/lua location /hello { default_type "text/plain"; content_by_lua 'ngx.say("Nginx Lua Hello!")'; } #GET http://test.lua.com/get?key=key location /get { set_unescape_uri keyarg_key; redis2_query get key; redis2_pass 127.0.0.1:6379; #配置redis访问 } #SET http://test.lua.com/set?key=key&val=value location /set { set_unescape_urikey arg_key; set_unescape_urival arg_val; redis2_query setkey $val; redis2_pass 127.0.0.1:6379; } } 重启Nginx /etc/init.d/nginx restart
也可以直接用lua调用redis的接口
local ckid = redis.pcall('get',KEYS[1]) local meta if ckid ~= nil then meta = redis.call('hgetall', ckid) else meta = 'none' ckid = 'none' end return {ckid, meta}
这里再分享一个比较完整的实例:
location /foo { set value 'first'; redis2_query set onevalue; redis2_pass 127.0.0.1:6379; } # GET /get?key=some_key location /get { set_unescape_uri keyarg_key; # this requires ngx_set_misc redis2_query get key; redis2_pass foo.com:6379; } # GET /set?key=one&val=first%20value location /set { set_unescape_urikey arg_key; # this requires ngx_set_misc set_unescape_urival arg_val; # this requires ngx_set_misc redis2_query setkey val; redis2_pass foo.com:6379; } # multiple pipelined queries location /foo { setvalue 'first'; redis2_query set one value; redis2_query get one; redis2_query set one two; redis2_query get one; redis2_pass 127.0.0.1:6379; } location /bar { # is not special here... redis2_literal_raw_query '*1\r\n4\r\nping\r\n'; redis2_pass 127.0.0.1:6379; } location /bar { # variables can be used below and is special redis2_raw_query 'get one\r\n'; redis2_pass 127.0.0.1:6379; } # GET /baz?get%20foo%0d%0a location /baz { set_unescape_uri queryquery_string; # this requires the ngx_set_misc module redis2_raw_query $query; redis2_pass 127.0.0.1:6379; } location /init { redis2_query del key1; redis2_query lpush key1 C; redis2_query lpush key1 B; redis2_query lpush key1 A; redis2_pass 127.0.0.1:6379; } location /get { redis2_query lrange key1 0 -1; redis2_pass 127.0.0.1:6379; }
如果你的逻辑比较简单推荐用lua试试,如果逻辑比较的复杂,那我觉得还是慎重点的,不然花费学习lua的时间还不如用python的tornado flask这样的web框架开个多进程来做端口轮训。
openresty挺好用的,就是没机会用。。