安装nginx參见《nginx+lua+redis构建高并发应用

让nginx 中的nginx_lua_module支持mysql 和memcache

下载

https://github.com/agentzh/lua-resty-memcached

https://github.com/agentzh/lua-resty-mysql

对于訪问接口的统一有非常多的处理方式,这里介绍使用nginx lua 訪问mysql并用memcache缓存起来。

配置例如以下:

        ...
location /getinfo {
default_type 'text/plain';
content_by_lua ' local args = ngx.req.get_uri_args() if args["appleid"] == nil then
ngx.say("param appleid is nil")
return
end local memcached = require "memcached"
local memc, err = memcached:new()
if not memc then
ngx.say("failed to instantiate memc: ", err)
return
end memc:set_timeout(1000) -- 1 sec local ok, err = memc:connect("172.16.18.114", 11211)
if not ok then
ngx.say("failed to connect: ", err)
return
end local res, flags, err = memc:get(args["appleid"])
if err then
ngx.say("failed to get memc: ", err)
return
end if not res then local mysql = require "mysql"
local db, err = mysql:new()
if not db then
ngx.say("failed to instantiate mysql: ", err)
return
end db:set_timeout(1000) -- 1 sec local ok, err, errno, sqlstate = db:connect{
host = "172.16.18.162",
port = 3306,
database = "test",
user = "root",
password = "cpyf",
max_packet_size = 1024 * 1024
} if not ok then
ngx.say("failed to connect: ", err, ": ", errno, " ", sqlstate)
return
end -- ngx.say("connected to mysql.") sql = "select * from tagval where tag = \'" .. args["appleid"] .. "\'" res, err, errno, sqlstate = db:query(sql)
if not res then
ngx.say("bad result: ", err, ": ", errno, ": ", sqlstate, ".")
return
end local json = require "json"
ngx.say("mysql found")
ngx.say(json.encode(res)) local ok, err = memc:set(args["appleid"], json.encode(res))
if not ok then
ngx.say("failed to set memc: ", err)
return
end local ok, err = db:set_keepalive(0, 100)
if not ok then
ngx.say("failed to set keepalive: ", err)
return
end return end ngx.say("memc found")
ngx.say(res) memc:set_keepalive(0, 100) ';
}
...

第二次执行:

curl --get http://app.ca-sim.com/getinfo?appleid=jfy

mysql found
[{"val":"123","tag":"jfy"}]

第二次后执行:

curl --get 

appleid=jfy">http://app.ca-sim.com/getinfo?

appleid=jfy

memc found
[{"val":"123","tag":"jfy"}]

结果已被缓存

在nginx中使用lua直接訪问mysql和memcaced达到数据接口的统一的更多相关文章

  1. CentOS6.4 安装OpenResty和Redis 并在Nginx中利用lua简单读取Redis数据

    1.下载OpenResty和Redis OpenResty下载地址:wget http://openresty.org/download/ngx_openresty-1.4.3.6.tar.gz Re ...

  2. 服务降级 托底预案 Nginx中使用Lua脚本检测CPU使用率,当达到阀值时开启限流,让用户排队

    https://mp.weixin.qq.com/s/FZAcQQAKomGEe95kln1HCQ 在京东我们是如何做服务降级的 https://mp.weixin.qq.com/s/FZAcQQAK ...

  3. PHP訪问MySql数据库 0基础篇

    在站点后台,常常要与数据库打交道.本文介绍怎样使用XAMPP来管理MySql数据库及怎样用PHP来訪问MySql数据库. 一.使用XAMPP来管理MySql数据库 首先使用XAMPP打开MySql的管 ...

  4. PHP的MySQL扩展:PHP訪问MySQL的经常使用扩展函数

    来源:http://www.ido321.com/1024.html 一.PHP连接数据库及基本操作 MySQL採用的是'客户机/server'架构.使用PHP安装的MySQL扩展函数,和直接使用cl ...

  5. mysql的安装、C++訪问mysql数据库、编码设置问题

    一.mysql的安装.这个相对简单,直接去官网下载mysql安装程序.就能够完毕安装过程,网上有非常多安装教程,这个没什么注意事项. 二.C++訪问mysql.主要是用到mysql定义的头文件,内部定 ...

  6. php訪问mysql数据库

    PHP訪问Mysql数据库 PHP能够通过mysql接口和mysqli接口訪问mysql数据库. 须要加入mysql和mysqli接口才干訪问mysql数据库. windows下配置amp: a.安装 ...

  7. 配置Nginx防止直接用IP訪问Webserver

    看了非常多Nginx的配置,好像都忽略了ip直接訪问Web的问题.这样理论上不利于SEO优化,所以我们希望能够避免直接用IP訪问站点.而是域名訪问.详细怎么做呢.看以下. 官方文档中提供的方法: If ...

  8. Java中怎么控制线程訪问资源的数量

    在API中是这样来描写叙述Semaphore 的 Semaphore 通经常使用于限制能够訪问某些资源(物理或逻辑的)的线程数目. 一个计数信号量.从概念上讲,信号量维护了一个许可集.如有必要,在许可 ...

  9. gcc的bug? c++模板类中友元函数的訪问权限问题

    原文地址:http://stackoverflow.com/q/23171337/3309790 在c++中,模板类中能够直接定义一个友元函数.该函数拥有訪问该模板类非public成员的权限. 比方: ...

随机推荐

  1. vue移动端上拉加载更多

    LoadMore.vue <template> <div class="load-more-wrapper" @touchstart="touchSta ...

  2. Office2010激活工具

    mini-KMS Activator是一款好用Office2010激活工具,“KMS”是微软对于“大客户”(VOL或VL)提供的Microsoft产品激活方式之一.在适用此方式的Microsoft产品 ...

  3. method initializationerror not found:JUnit4单元測试报错问题

           今天使用JUnit 4进行单元測试时,測试程序一直执行不起来,报method initializationerror not found错误.例如以下:            网上说版本 ...

  4. Android Unable to execute dex: method ID not in [0, 0xffff]: 65536 问题解决方法

    开始一个新项目的时候,Build工程的时候一直报这个错误: 控制台报错误:Conversion to Dalvik format failed: Unable to execute dex: meth ...

  5. visual studio code 中 debugger for chrome 插件的配置

    安装 debugger for chrome 插件后,把默认的 launch.json 改成: { "name": "谷歌浏览器", "type&qu ...

  6. 15、python学习手册之:列表和字典

    1.列表属于可变序列,支持在原处的修改 2.在标准python解锁器内部,列表就是C数组而不是链接结构 3.内置函数map对序列中的各项应用一个函数并把结果收集到一个新的列表中 eg:list(map ...

  7. eclipse 使用jetty调试时,加依赖工程的源码调试方法

    [1] 添加source eclipse-->debug as-->debug configurations-->source [2]若source不起作用 重新编译一下,mvn c ...

  8. 如何把别人的原理图和pcb图建立一个完整的工程

    这里是我从网友那里下载的pcb图和原理图 我们怎么通过这两个文件建立一个完整的工程 我们选中pcb图文件,通过下面的操作,就可以导出pcb封装库: 同样的方法,我选中pcb图,然后用下面图的方法,就可 ...

  9. 洛谷——P1011 车站

    https://www.luogu.org/problem/show?pid=1011#sub 题目描述 火车从始发站(称为第1站)开出,在始发站上车的人数为a,然后到达第2站,在第2站有人上.下车, ...

  10. FTP中的授权规则

    在授权规则中,你可以管理自己的FTP站点以怎样的方式进行访问,比如每个进入站点的人都需要输入用户名密码.正则可以在授权规则中删除默认的配置“允许匿名用户读取”的规则. 也可以在此处,对不同的组或用户进 ...