毕竟redis是作为缓存,供程序的快速读写,虽然reidis也可以做持久化保存,但还是需要一个做数据存储的数据库。比如首次查询数据在redis查询不到则查询mysql,再将查询结果写过redis供下次查询。保存数据也可以先写入redis再通过队列使用另外的程序异步写入mysql。
后面会逐步把openresty写入rabbitmq和python通过订阅rabbitmq的队列将数据写入mysql得方法都整理一下。
先看看openresty如何对mysql进行连接和操作,并进行二次封装
mysql操作流程
通过resty.mysql库进行连接和操作
local mysql = require("resty.mysql")
local client, errmsg = mysql:new()
if not client then
return false, "mysql.socket_failed: " .. (errmsg or "nil")
end
client:set_timeout(10000)
local options = {
host = "127.0.0.1",
port = 3306,
user = "root",
password = "root",
database = "test"
}
local result, errmsg, errno, sqlstate = client:connect(options)
if not result then
return