OpenResty--mysql,redis 项目中的应用
最近刚刚接手同事的OpenResty的项目,发现对mysql,redis的操作没有用连接池,故对此进行了改造。
MYSQL
主要是通过mysql_pool.lua 和 dbutil.lua 来封装对数据库的操作
mysql_pool.lua:
module("mysql_pool", package.seeall)
local dbConfig = require"config"
local mysql = require("resty.mysql")
local mysql_pool = {}
--[[
先从连接池取连接,如果没有再建立连接.
返回:
false,出错信息.
true,数据库连接
--]]
function mysql_pool:get_connect()
if ngx.ctx[mysql_pool] then
return true, ngx.ctx[mysql_pool]
end
local client, errmsg = mysql:new()
if not client then
return false, "mysql.socket_failed: " .. (errmsg or "nil")
end
client:set_timeout() --10秒
local options = {
host = dbConfig.DBHOST,
port = dbConfig.DBPORT,
user = dbConfig.DBUSER,
password = dbConfig.DBPASSWORD,
database = dbConfig.DBNAME
}
local result, errmsg, errno, sqlstate = client:connect(options)
if not result then
return false, "mysql.cant_connect: " .. (errmsg or "nil") .. ", errno:" .. (errno or "nil") ..
", sql_state:" .. (sqlstate or "nil")
end
local query = "SET NAMES " .. dbConfig.DEFAULT_CHARSET
local result, errmsg, errno, sqlstate = client:query(query)
if not result then
return false, "mysql.query_failed: " .. (errmsg or "nil") .. ", errno:" .. (errno or "nil") ..
", sql_state:" .. (sqlstate or "nil")
end
ngx.ctx[mysql_pool] = client
return true, ngx.ctx[mysql_pool]
end
--[[
把连接返回到连接池
用set_keepalive代替close() 将开启连接池特性,可以为每个nginx工作进程,指定连接最大空闲时间,和连接池最大连接数
--]]
function mysql_pool:close()
if ngx.ctx[mysql_pool] then
ngx.ctx[mysql_pool]:set_keepalive(, )
ngx.ctx[mysql_pool] = nil
end
end
--[[
查询
有结果数据集时返回结果数据集
无数据数据集时返回查询影响
返回:
false,出错信息,sqlstate结构.
true,结果集,sqlstate结构.
--]]
function mysql_pool:query(sql, flag)
local ret, client = self:get_connect(flag)
if not ret then
return false, client, nil
end
local result, errmsg, errno, sqlstate = client:query(sql)
self:close()
if not result then
errmsg = concat_db_errmsg("mysql.query_failed:", errno, errmsg, sqlstate)
return false, errmsg, sqlstate
end
return true, result, sqlstate
end
return mysql_pool
dbutil.lua
module("dbutil", package.seeall)
local mysql_pool = require("mysql_pool")
function query(sql)
local ret, res, _ = mysql_pool:query(sql)
if not ret then
ngx.log(ngx.ERR, "query db error. res: " .. (res or "nil"))
return nil
end
return res
end
function execute(sql)
local ret, res, sqlstate = mysql_pool:query(sql)
if not ret then
ngx.log(ngx.ERR, "mysql.execute_failed. res: " .. (res or 'nil') .. ",sql_state: " .. (sqlstate or 'nil'))
return -
end
return res.affected_rows
end
REDIS
redis_pool.lua:
module("redis_pool", package.seeall)
local redisConfig = require"config"
local redis = require("resty.redis")
local redis_pool = {}
--[[
先从连接池取连接,如果没有再建立连接.
返回:
false,出错信息.
true,redis连接
--]]
function redis_pool:get_connect()
if ngx.ctx[redis_pool] then
return true, ngx.ctx[redis_pool]
end
local client, errmsg = redis:new()
if not client then
return false, "redis.socket_failed: " .. (errmsg or "nil")
end
client:set_timeout() --10秒
local result, errmsg = client:connect(redisConfig.REDIS_HOST, redisConfig.REDIS_PORT)
if not result then
return false, errmsg
end
ngx.ctx[redis_pool] = client
return true, ngx.ctx[redis_pool]
end
function redis_pool:close()
if ngx.ctx[redis_pool] then
ngx.ctx[redis_pool]:set_keepalive(, )
ngx.ctx[redis_pool] = nil
end
end
return redis_pool
OpenResty--mysql,redis 项目中的应用的更多相关文章
- 如何基于 Docker 快速搭建 Springboot + Mysql + Redis 项目
目录 前言 项目目录 搭建项目 1. docker安装启动mysql以及redis 1.1 安装mysql 1.2 安装redis 2. 初始化数据库 3.创建项目 4.初始化代码 4.1 全局配置文 ...
- 缓存框架有使用过哪些?memcache和redis有什么区别?项目中,怎么去选择?
缓存有:ehcache,memcache和redis等 区别: 1. Redis和Memcache都是将数据存放在内存中,都是内存数据库.不过memcache还可用于缓存其他东西,例如图片.视频等等. ...
- 具体解释Redis源代码中的部分高速排序算法(pqsort.c)
看标题.你可能会疑惑:咦?你这家伙.怎么不解说完整的快排,仅仅讲一部分快排---.- 哎,冤枉. "部分快排"是算法的名字.实际上本文相当具体呢.本文差点儿与普通快排无异.看懂了本 ...
- openresty开发系列28--openresty中操作mysql
openresty开发系列28--openresty中操作mysql Mysql客户端 应用中最常使用的就是数据库了,尤其mysql数据库,那openresty lua如何操作mysql呢? ...
- openresty开发系列27--openresty中封装redis操作
openresty开发系列27--openresty中封装redis操作 在关于web+lua+openresty开发中,项目中会大量操作redis, 重复创建连接-->数据操作-->关闭 ...
- 从 0 使用 SpringBoot MyBatis MySQL Redis Elasticsearch打造企业级 RESTful API 项目实战
大家好!这是一门付费视频课程.新课优惠价 699 元,折合每小时 9 元左右,需要朋友的联系爱学啊客服 QQ:3469271680:我们每课程是明码标价的,因为如果售价为现在的 2 倍,然后打 5 折 ...
- ubuntu18.04+gunicorn+nginx+supervisor+mysql+redis安装django项目
Ubuntu18.04 install Django project 项目准备: ECS 实例 (云服务器) 此安装部署方案适合本地ubuntu18.04系统安装和虚拟机中ubuntu18.04系统安 ...
- 【新手总结】在.Net项目中使用Redis作为缓存服务
最近由于项目需要,在系统缓存服务部分上了redis,终于有机会在实际开发中玩一下,之前都是自己随便看看写写,很零碎也没沉淀下来什么,这次算是一个系统学习和实践过程的总结. 和Redis有关的基础知识 ...
- spring3.0结合Redis在项目中的运用
推荐一个程序员的论坛网站:http://ourcoders.com/home/ 以下内容使用到的技术有:Redis缓存.SpringMVC.Maven.项目中使用了redis缓存,目的是在业务场景中, ...
随机推荐
- POJ 3260 The Fewest Coins(多重背包问题, 找零问题, 二次DP)
Q: 既是多重背包, 还是找零问题, 怎么处理? A: 题意理解有误, 店主支付的硬币没有限制, 不占额度, 所以此题不比 1252 难多少 Description Farmer John has g ...
- WebService远程调用(代码调用)
在做多个系统集成的时候,由于各系统厂商采用不同的架构,在项目实施前期,各业务对业务理解不够深入,系统接口可能会有较多变化, 在此背景下,动态调用webserivce就变得灵活了,降低了系统集成的耦合度 ...
- ios开发之--UIDocumentInteractionController的使用(实现更多分享服务)
最近在做项目的时候,碰到这样一个需求,就是本地生成pdf文件,然后本地打开,经过测试发现,pdf文件是无法保存到相册里面的,只能存到手机里面,鉴于苹果的存储机制,需要取出来,进行本地展示,可以直接传到 ...
- 给IT同学推荐这15个不错的学习网站,收藏起来慢慢看吧
1.学堂在线:http://www.xuetangx.com 目前,学堂在线运行了包括包括清华大学.北京大学.复旦大学.斯坦福大学.麻省理工学院.加州大学伯克利分校等国内外几十所顶尖高校的优质课程.在 ...
- 浏览器Chrome对WebGL支持判断
1.开启方式: 第一种:打开cmd,切换到Chorme的安装目录,敲入chrome.exe --enable -webgl,回车就会打开一个chrome浏览器窗口: 第二种:找到Chrome浏览器的快 ...
- ionic creator(ionic生成器)
用来生成前端 html 还是挺方便的(接口数据另算),弄好就可以直接下载 https://creator.ionic.io/app/dashboard/projects
- 当singleton Bean依赖propotype Bean,可以使用在配置Bean添加look-method来解决
在Spring里面,当一个singleton bean依赖一个prototype bean,因为singleton bean是单例的,因此prototype bean在singleton bean里面 ...
- linux配置了dns后导致mysql远程连接慢问题
有时候dns配置错误或者其它原因会导致mysql远程连接缓慢,此时只需要关闭mysqlDNS反向解析即可解决此问题. 打开my.cnf配置,将[mysqld]下的#skip-name-resolve前 ...
- Django学习笔记 开发环境搭建
为什么使用django?1.支持快速开发:用python开发:数据库ORM系统,并不需要我们手动地构造SQL语句,而是用python的对象访问数据库,能够提升开发效率.2.大量内置应用:后台管理系统a ...
- CentOS中用户不在 sudoers 文件中。此事将被报告。
首先切换为root用户 su root; 然后更改etc/sudoers文件的只读为可读可写可执行. sudo chmod 777 /etc/sudoers 输入visudo命令即可编辑文件,找到ro ...