lua nginx mysql_使用lua从mysql数据库导出数据到redis与Nginx缓存

本文介绍了如何在Linux环境下安装Lua和OpenResty,通过Lua脚本从MySQL数据库查询数据,并将结果存储到Redis。接着,展示了如何利用Nginx读取Redis中的数据作为本地缓存,实现数据的快速访问。同时,文章还给出了在Nginx配置文件中设置相应location的示例。
摘要由CSDN通过智能技术生成

安装lua

有linux版本的安装也有mac版本的安装。。我们采用linux版本的安装,首先我们准备一个linux虚拟机。

安装步骤,在linux系统中执行下面的命令。

//下载

curl -R -O http://www.lua.org/ftp/lua-5.3.5.tar.gz

//解压

tar zxf lua-5.3.5.tar.gz

cd lua-5.3.5

make linux test

此时再执行lua测试看lua是否安装成功

[root@localhost ~]# lua

Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, PUC-Rio

安装openresty

linux安装openresty:

1.添加仓库执行命令

yum install yum-utils

yum-config-manager --add-repo https://openresty.org/package/centos/openresty.repo

2.执行安装

yum install openresty

3.安装成功后 会在默认的目录如下:

/usr/local/openresty

测试访问

重启下centos虚拟机,然后访问测试Nginx

访问地址:http://192.168.200.128/

lua脚本从数据库查询数据到redis,创建update_ad.lua

ngx.header.content_type="application/json;charset=utf8"

local cjson = require("cjson")

local mysql = require("resty.mysql")

local uri_args = ngx.req.get_uri_args()

//地址传来的参数

local position = uri_args["position"]

local db = mysql:new()

db:set_timeout(10000)

local props = {

host = "192.168.200.128",

port = 3306,

database = "数据库名",

user = "root",

password = "root"

}

local res = db:connect(props)

local select_sql = "select url,image from tb_ad where status ='1' and position='"..position.."' and start_time<= NOW() AND end_time>= NOW()"

res = db:query(select_sql)

db:close()

local redis = require("resty.redis")

local red = redis:new()

red:set_timeout(2000)

local ip ="192.168.200.128"

local port = 6379

red:connect(ip,port)

red:set("ad_"..position,cjson.encode(res))

red:close()

ngx.say("{flag:true}")

数据从redis到nginx,创建read_ad.lua

--设置响应头类型

ngx.header.content_type="application/json;charset=utf8"

--获取请求中的参数ID

local uri_args = ngx.req.get_uri_args();

local position = uri_args["position"];

--获取本地缓存

local cache_ngx = ngx.shared.dis_cache;

--根据ID 获取本地缓存数据

local adCache = cache_ngx:get('ad_cache_'..position);

if adCache == "" or adCache == nil then

--引入redis库

local redis = require("resty.redis");

--创建redis对象

local red = redis:new()

--设置超时时间

red:set_timeout(2000)

--连接

local ok, err = red:connect("192.168.200.128", 6379)

--获取key的值

local rescontent=red:get("ad_"..position)

--输出到返回响应中

ngx.say(rescontent)

--关闭连接

red:close()

--将redis中获取到的数据存入nginx本地缓存

cache_ngx:set('ad_cache_'..position, rescontent, 10*60);

else

--nginx本地缓存中获取到数据直接输出

ngx.say(adCache)

end

在/usr/local/openresty/nginx/conf/nginx.conf中添加头信息,和 location信息

server {

listen 80;

server_name localhost;

location /update_ad {

content_by_lua_file /root/lua/update_ad.lua;

}

location /ad_read {

#使用限流配置 burst 突发情况下 允许访问4个请求

limit_req zone=adRateLimit burst=4 nodelay;

content_by_lua_file /usr/local/openresty/lua/read_ad.lua;

}

}

如果要尝试访问可将前端页面放到/usr/local/openresty/nginx/html

下。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值