最近刚刚接手同事的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 项目中的应用的更多相关文章

  1. 如何基于 Docker 快速搭建 Springboot + Mysql + Redis 项目

    目录 前言 项目目录 搭建项目 1. docker安装启动mysql以及redis 1.1 安装mysql 1.2 安装redis 2. 初始化数据库 3.创建项目 4.初始化代码 4.1 全局配置文 ...

  2. 缓存框架有使用过哪些?memcache和redis有什么区别?项目中,怎么去选择?

    缓存有:ehcache,memcache和redis等 区别: 1. Redis和Memcache都是将数据存放在内存中,都是内存数据库.不过memcache还可用于缓存其他东西,例如图片.视频等等. ...

  3. 具体解释Redis源代码中的部分高速排序算法(pqsort.c)

    看标题.你可能会疑惑:咦?你这家伙.怎么不解说完整的快排,仅仅讲一部分快排---.- 哎,冤枉. "部分快排"是算法的名字.实际上本文相当具体呢.本文差点儿与普通快排无异.看懂了本 ...

  4. openresty开发系列28--openresty中操作mysql

    openresty开发系列28--openresty中操作mysql Mysql客户端   应用中最常使用的就是数据库了,尤其mysql数据库,那openresty lua如何操作mysql呢?   ...

  5. openresty开发系列27--openresty中封装redis操作

    openresty开发系列27--openresty中封装redis操作 在关于web+lua+openresty开发中,项目中会大量操作redis, 重复创建连接-->数据操作-->关闭 ...

  6. 从 0 使用 SpringBoot MyBatis MySQL Redis Elasticsearch打造企业级 RESTful API 项目实战

    大家好!这是一门付费视频课程.新课优惠价 699 元,折合每小时 9 元左右,需要朋友的联系爱学啊客服 QQ:3469271680:我们每课程是明码标价的,因为如果售价为现在的 2 倍,然后打 5 折 ...

  7. ubuntu18.04+gunicorn+nginx+supervisor+mysql+redis安装django项目

    Ubuntu18.04 install Django project 项目准备: ECS 实例 (云服务器) 此安装部署方案适合本地ubuntu18.04系统安装和虚拟机中ubuntu18.04系统安 ...

  8. 【新手总结】在.Net项目中使用Redis作为缓存服务

    最近由于项目需要,在系统缓存服务部分上了redis,终于有机会在实际开发中玩一下,之前都是自己随便看看写写,很零碎也没沉淀下来什么,这次算是一个系统学习和实践过程的总结. 和Redis有关的基础知识 ...

  9. spring3.0结合Redis在项目中的运用

    推荐一个程序员的论坛网站:http://ourcoders.com/home/ 以下内容使用到的技术有:Redis缓存.SpringMVC.Maven.项目中使用了redis缓存,目的是在业务场景中, ...

随机推荐

  1. 如何调用别人发布的WebService程序

    这篇经验会告诉我们如何调用别人发布的WebService,并且需要注意的事项.现在就拿获取天气预报的接口举例,因为文中不允许有链接,所以在下文图中有WebService链接的地址. 工具/原料   V ...

  2. 怎样设置table中td的高度为1px

    在制作edm时会遇到须要设置td的高度为1px,假设td标签中有 时不管你怎么设置td的高度都没用,最小高度都是18px. 这时须要把表格中的 去掉.例: 原来是这种: <tr> < ...

  3. Bitmap之安卓手机壁纸的设置

    MainActivity: package com.hyzhou.imagedemo; import java.io.File; import android.os.Bundle; import an ...

  4. YARN的设计

    YARN:下一代 Hadoop 计算平台 我们现在稍微改变一下用辞.以下名称的改动有助于更好地了解 YARN 的设计: ResourceManager 代替集群管理器 ApplicationMaste ...

  5. redis的初认识

    Redis是一个开源,先进的key-value存储,并用于构建高性能,可扩展的Web应用程序的完美解决方案. Redis从它的许多竞争继承来的三个主要特点: Redis数据库完全在内存中,使用磁盘仅用 ...

  6. 【RF库Collections测试】Dictionaries Should Be Equal

    Name:Dictionaries Should Be EqualSource:Collections <test library>Arguments:[ dict1 | dict2 | ...

  7. Android package属性、package name和Application ID三者的联系及区别

    package属性:在AndroidManifest.xml文件中. package name:模块结构的包名. Application ID:模块defaultConfig块下的applicatio ...

  8. PHP创建文件以及移动文件

    创建文件,这里用到的是fopen,即是打开,又是创建 <?php $counter_file = 'aa.txt ';//文件名及路径,在当前目录下新建aa.txt文件 $fopen = fop ...

  9. nginx 重复提交 重复请求问题

    我遇到的奇葩问题. 后台使用的是nginx + tomcat 前端页面确实只发送了一个ajax请求到后台. 后台却接收到了两条请求! 百度了一下,说是因为nginx负载均衡,一个请求超时后会重复发送一 ...

  10. 在fedora 18中将普通用户设置为sudo权限

    将一般的用户加入sudo组is not in the sudoers file. This incident will be reported 解决方法 在一般用户下执行sudo命令提示xxx is ...