需要先安装好OpenResty
然后对nginx的配置进行设置:
添加配置
lua_shared_dict my_cache 128m;
以及
location /update_content {
content_by_lua_file /root/lua/update_content.lua;
#需要在相应位置添加lua脚本 lua脚本的编写在下面 自己改改就可以了
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
lua_shared_dict my_cache 128m;
server {
listen 80;
server_name localhost;
#lua_code_cache on;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
#root html;
#index index.html index.htm;
#content_by_lua_block {
# ngx.say("hello world");
#}
default_type text/html;
content_by_lua_file /root/lua/test3.lua;
}
location /update_content {
content_by_lua_file /root/lua/update_content.lua;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
lua脚本
-- 设置编码以及格式
ngx.header.content_type="application/json;charset=utf8"
-- 跟nginx里面配置的lua_shared_dict my_cache 128m; 相呼应
local cache_ngx = ngx.shared.my_cache
-- 获取mysql组件
local mysql = require("resty.mysql")
-- 获取json组件
local cjson = require("cjson")
-- 获取请求中的参数数组
local args = ngx.req.get_uri_args()
-- 获取其中的id
local id = args["id"]
-- nginx缓存获取数据
local contentCache = cache_ngx:get("content_cache_"..id)
-- 输出瞧瞧
-- ngx.say(contentCache)
-- 判断以下 不存在就走redis 存在就直接输出
if contentCache == "" or contentCache == nil then
-- 说明不存在 走redis
local redis = require "resty.redis"
-- 创建redis
local red = redis:new()
red:set_timeouts(1000, 1000, 1000) -- 1 sec
local ok, err = red:connect("127.0.0.1", 6379)
if not ok then
ngx.say("failed to connect: ", err)
return
end
local res, err = red:get("content_"..id)
-- ngx.say(res)
if not res then
ngx.say("failed to get: ", err)
return
end
-- redis也没有走mysql
if res == ngx.null then
--创建mysql连接实例
local db, err = mysql:new()
if not db then
ngx.say("new mysql error : ", err)
return
end
--设置超时时间(毫秒)
db:set_timeout(1000)
-- 定义链接属性
local props = {
host = "127.0.0.1",
port = 3306,
database = "changgou_goods",
user = "root",
password = "lzhlove520"
}
-- 发起连接
local res, err, errno, sqlstate = db:connect(props)
if not res then
ngx.say("connect to mysql error : ", err, " , errno : ", errno, " , sqlstate : ", sqlstate)
return close_db(db)
end
--查询
local select_sql = "SELECT * FROM `student` where id = "..id..""
res, err, errno, sqlstate = db:query(select_sql)
if not res then
ngx.say("select error : ", err, " , errno : ", errno, " , sqlstate : ", sqlstate)
return close_db(db)
end
--输出 json
local str = cjson.encode(res)
-- 数据存入redis
ok, err = red:set("content_"..id, str)
-- mysql数据直接输出
ngx.say(str)
-- 关闭mysql链接
local function close_db(db)
if not db then
return
end
db:close()
end
close_db(db)
return
end
-- redis有数据 直接输出
ngx.say(res)
-- 设置nginx缓存
local succ, err, forcible = cache_ngx:set("content_cache_"..id, res, 1*10)
else
-- nginx有数据直接输出
ngx.say(contentCache)
end
启动测试即可。
取消其中的一些ngx.say()输出注释就可以看到测试结果了。