Nginx + Lua + Redis high performance cache data read

This article is taken:  https://segmentfault.com/p/1210000011625271/read

Before Lua not used, we obtain data from redis path route retrieval and comparison data after using lua, obvious that improving efficiency.

 

 

 

Installation OpenResty

Reference yum given official mounting step, various support systems have, also take the form of source installation, the installation is complete default path is / usr / local / openresty, the new version of the operating module OpenResty Redis comes, so we need reinstall your own.

Configuring nginx

Increase follows below http module

lua_package_path "/usr/local/openresty/lualib/?.lua;;"; #lua 模块
lua_package_cpath "/usr/local/openresty/lualib/?.so;;"; #c模块

Lua configured for better configuration, independent lua.conf document does not nginx.conf stir together with, lua.con configuration file as follows:

#lua.conf
server {
listen 80;
server_name _;
}

In nginx.conf file http module will be introduced

includes lua.conf;

Simple test

Write simple lua script file test.lua, storage directory is conf / lua below

ngx.say("hello lua world");

Modify lua.conf

location /lua {
default_type 'text/html'; 

lua_code_cache off; 

content_by_lua_file conf / Contact / test.lua;
}

Test configuration is correct

./nginx -t # detect configuration file is correct, it means that the log displays the following success

nginx: [alert] lua_code_cache is off; this will hurt performance in/usr/local/nginx/conf/lua.conf:7
nginx: [alert] lua_code_cache is off; this will hurt performance in/usr/local/nginx/conf/lua.conf:13
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

Enter http://192.168.1.105/lua in the browser, the page normal output "hello lua world"

JSON support

Scripts address http://lua-users.org/wiki/JsonModules

Normal Gets string type value there is no problem, we get json format of the key value needs JSON support in order to function properly. Download the script to place it under / usr / local / openresty / lualib directory for reference in lua script

Data acquisition redis

Redis connection test scripts written, and get the value of a specified key from the redis. Script reads as follows:

local redis = require("resty.redis")
local json = require ("dkjson")

--创建实例
local redis_instance = redis:new()
--设置超时(毫秒)
redis_instance:set_timeout(3000)
--建立连接
local host = "127.0.0.1"
local port = 6679
local ok, err = redis_instance:connect(host, port)
if not ok then
ngx.say("connect to redis error : ", err)
return close_redis(redis_instance)
end
local resp, err = redis_instance:eval("return redis.call('get', KEYS[1])", 1, "alibaba");
ngx.say("redis data = ",resp);

ngx.say("redis data = ",resp); ngx.say("json data = ",json.encode(resp))

- normal circumstances should have closed redis, where only a simple test case, without making close

Configuration lua.conf, as follows

location /lua_redis_test {
default_type 'text/html';
lua_code_cache off;
content_by_lua_file /usr/local/nginx/conf/lua/json_test.lua;
}

The value written in redis alibaba bond, used herein can write simple jedis

Jedis redis = new Jedis("192.168.1.105", 6679); 

JSONObject object = new JSONObject(); 

object.put("aaaa", 123); 

object.put("bbbbb", 23234); 

redis.set("alibaba", object.toString());

After the test configuration is correct, restart nginx. Browser input http://192.168.1.105/lua_redis_test, alibaba key values ​​should redis output.

redis data = {"aaaa":123,"bbbbb":23234}json data = "{"aaaa":123,"bbbbb":23234}

At this point based on nginx, you can simply get the data from redis by lua script, greatly improving the efficiency of data requests a response.

Guess you like

Origin www.cnblogs.com/123-shen/p/11504195.html