首页 > 代码库 > OpenResty--mysql,redis 项目中的应用

OpenResty--mysql,redis 项目中的应用

最近刚刚接手同事的OpenResty的项目,发现对mysql,redis的操作没有用连接池,故对此进行了改造。

 

MYSQL

主要是通过mysql_pool.lua 和 dbutil.lua 来封装对数据库的操作

mysql_pool.lua:

 1 module("mysql_pool", package.seeall) 2  3 local dbConfig = require"config" 4 local mysql = require("resty.mysql") 5  6 local mysql_pool = {} 7  8 --[[ 9     先从连接池取连接,如果没有再建立连接.10     返回:11         false,出错信息.12         true,数据库连接13 --]]14 function mysql_pool:get_connect()15     if ngx.ctx[mysql_pool] then16         return true, ngx.ctx[mysql_pool]17     end18 19     local client, errmsg = mysql:new()20     if not client then21         return false, "mysql.socket_failed: " .. (errmsg or "nil")22     end23 24     client:set_timeout(10000)  --10秒25 26     local options = {27         host = dbConfig.DBHOST,28         port = dbConfig.DBPORT,29         user = dbConfig.DBUSER,30         password = dbConfig.DBPASSWORD,31         database = dbConfig.DBNAME32     }33 34     local result, errmsg, errno, sqlstate = client:connect(options)35     if not result then36         return false, "mysql.cant_connect: " .. (errmsg or "nil") .. ", errno:" .. (errno or "nil") ..37                 ", sql_state:" .. (sqlstate or "nil")38     end39 40     local query = "SET NAMES " .. dbConfig.DEFAULT_CHARSET41     local result, errmsg, errno, sqlstate = client:query(query)42     if not result then43         return false, "mysql.query_failed: " .. (errmsg or "nil") .. ", errno:" .. (errno or "nil") ..44                 ", sql_state:" .. (sqlstate or "nil")45     end46 47     ngx.ctx[mysql_pool] = client48     return true, ngx.ctx[mysql_pool]49 end50 51 --[[52     把连接返回到连接池53     用set_keepalive代替close() 将开启连接池特性,可以为每个nginx工作进程,指定连接最大空闲时间,和连接池最大连接数54  --]]55 function mysql_pool:close()56     if ngx.ctx[mysql_pool] then57         ngx.ctx[mysql_pool]:set_keepalive(60000, 1000)58         ngx.ctx[mysql_pool] = nil59     end60 end61 62 --[[63     查询64     有结果数据集时返回结果数据集65     无数据数据集时返回查询影响66     返回:67         false,出错信息,sqlstate结构.68         true,结果集,sqlstate结构.69 --]]70 function mysql_pool:query(sql, flag)71     local ret, client = self:get_connect(flag)72     if not ret then73         return false, client, nil74     end75     76     local result, errmsg, errno, sqlstate = client:query(sql)77     self:close()78 79     if not result then80         errmsg = concat_db_errmsg("mysql.query_failed:", errno, errmsg, sqlstate)81         return false, errmsg, sqlstate82     end83 84     return true, result, sqlstate85 end86 87 return mysql_pool

 dbutil.lua

 1 module("dbutil", package.seeall) 2 local mysql_pool = require("mysql_pool") 3  4 function query(sql) 5  6     local ret, res, _ = mysql_pool:query(sql) 7     if not ret then 8         ngx.log(ngx.ERR, "query db error. res: " .. (res or "nil")) 9         return nil10     end11 12     return res13 end14 15 function execute(sql)16 17     local ret, res, sqlstate = mysql_pool:query(sql)18     if not ret then19         ngx.log(ngx.ERR, "mysql.execute_failed. res: " .. (res or nil) .. ",sql_state: " .. (sqlstate or nil))20         return -121     end22 23     return res.affected_rows24 end
View Code

 

REDIS

redis_pool.lua:

 1 module("redis_pool", package.seeall) 2  3 local redisConfig = require"config" 4 local redis = require("resty.redis") 5  6 local redis_pool = {} 7  8 --[[ 9     先从连接池取连接,如果没有再建立连接.10     返回:11         false,出错信息.12         true,redis连接13 --]]14 function redis_pool:get_connect()15     if ngx.ctx[redis_pool] then16         return true, ngx.ctx[redis_pool]17     end18 19     local client, errmsg = redis:new()20     if not client then21         return false, "redis.socket_failed: " .. (errmsg or "nil")22     end23 24     client:set_timeout(10000)  --10秒25 26     local result, errmsg = client:connect(redisConfig.REDIS_HOST, redisConfig.REDIS_PORT)27     if not result then28         return false, errmsg29     end30 31     ngx.ctx[redis_pool] = client32     return true, ngx.ctx[redis_pool]33 end34 35 function redis_pool:close()36     if ngx.ctx[redis_pool] then37         ngx.ctx[redis_pool]:set_keepalive(60000, 300)38         ngx.ctx[redis_pool] = nil39     end40 end41 42 return redis_pool