最近刚刚接手同事的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. gevent动态随时添加任务

    关于爬虫,有scrapy框架,也有requests加协程 协程 进程的方法. 相关的包很多,比如threading .threadpool.multiprocessing,还有threadpoolex ...

  2. python2.0_day18_django_admin

    Django admin的个性化定制首先我们看下,前面章节中定义的models在admin后台管理界面的样子: 然后我们看下老男孩教育点名平台的admin管理表的后台界面样子: admin管理后台常用 ...

  3. 虚拟机如何装LINUX

    VMware 提供了免費的虛擬機 VMware player 5.0.2 供使用者下載. 從 VMware 官網http://www.vmware.com/. 的頁面進入 “Products”  “ ...

  4. 【java】将List中的实体按照某个字段进行分组的算法

    如何将List中存放的实体按照某个字段进行分组呢?来看看下面的例子,假如实体中有个字段叫批次号,我们将具有相同批次号的实体放在一起,那么怎么实现呢?看下面的代码: 可以定义个Map,Map的key用于 ...

  5. redis学习之集群报错Node is not empty

    遇到的问题及解决办法 在redis.conf里bind 真机ip后,接着重新执行每个redis.conf,最后再创建集群,但报错,如下图所示: 图中报的错即: [ERR] Node 192.168.1 ...

  6. 上传控件CSS用图片代替

    <style type="text/css"> a.btn {width: 120px;height: 42px;overflow: hidden;display: b ...

  7. 原创Java多线程详解(一)

    只看书不实践是不行的.来实践一下~~~~~~(引用请指明来源) 先看看百科对多线程的介绍 多线程(英语:multithreading),是指从软件或者硬件上实现多个线程并发执行的技术.具有多线程能力的 ...

  8. kerberos认证协议分析

    Kerberos认证协议分析 Kerberos认证协议流程 如上图: * 第一步:client和认证服务器(AS)通信完成认证过程,如果认证成功AS返回给client一个TGT(用来向TGS获取tic ...

  9. SSH电力项目二

    底层方法封装(CommonDaoImpl类) public class CommonDaoImpl<T> extends HibernateDaoSupport implements IC ...

  10. 170512、java日志文件log4j.properties配置详解

    一.Log4j配置 第一步:加入log4j-1.2.8.jar到lib下. 第二步:在CLASSPATH下建立log4j.properties.内容如下: 放在src下的话就不用配置 否则得去web. ...