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缓存,目的是在业务场景中, ...
随机推荐
- Java精选笔记_文件上传与下载
文件上传与下载 如何实现文件上传 在Web应用中,由于大多数文件的上传都是通过表单的形式提交给服务器的,因此,要想在程序中实现文件上传的功能,首先得创建一个用于提交上传文件的表单页面. 为了使Serv ...
- js检查浏览器是否处于隐身模式
网上大部分的文章写隐身模式下 localStorage 对象不可用,直接以 localStorage 能否写入来判断浏览器是否处于隐身模式其实是错的,在隐身模式下localStorage也是能使用的, ...
- lower()
lower() 用于把字符串中的大写字母转换成小写字母 In [1]: str = "Hello World" In [2]: str.lower() Out[2]: 'hello ...
- Lua脚本和C++交互(二)
上一节讲了一些基本的Lua应用,下面,我要强调一下,Lua的栈的一些概念,因为这个确实很重要,你会经常用到.熟练使用Lua,最重要的就是要时刻知道什么时候栈里面的数据是什么顺序,都是什么.如果你能熟练 ...
- js获取一个字符串中指定字符第n次出现的位置
function nthIndexOf(str,c,n){ var x=str.indexOf(c); for(var i=0;i<num;i++){ x=str.indexOf(c,x+1); ...
- Day05_C操作符及二进制补码计算
回顾: 1.数据类型 2.二进制(八进制,十六进制) --------------------------------------------------------- 计算机中不可以使用负号表示 ...
- Activity 设置切换动画
extends://blog.csdn.net/luohaowang320/article/details/42124225 | http://blog.csdn.net/xuewater/artic ...
- POM(project Object Model) Maven包管理依赖 pom.xml文件
什么是POM POM全称为“Project Object Model”,意思是工程对象模型.Maven工程使用pom.xml来指定工程配置信息,和其他文本信息.该配置文件以xml为格式,使用xml语法 ...
- Spring MVC 框架学习
一.spirng的简介 Spring是一个开源框架,它由Rod Johnson创建.它是为了解决企业应用开发的复杂性而创建的.Spring使用基本的JavaBean来完成以前只可能由EJB完成的事情. ...
- [Linux]ssh相关问题
ssh链接不上的最可能原因是防火墙没关,一般提示“connection refused”. 可以使用这个命令查看:#service iptables status 暂时关闭iptables,重启后还会 ...