安装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. CentOS 7 网络配置、远程访问

    网络配置(配置固定IP访问) 相关命令 ip add 查看网卡状态 ifup eth0 打开端口eth0 ifdown eth0 关闭端口eth0 dhclient 自动获取IP mii-tool e ...

  2. 洛谷 P1781 宇宙总统

    P1781 宇宙总统 题目背景 宇宙总统竞选 题目描述 地球历公元6036年,全宇宙准备竞选一个最贤能的人当总统,共有n个非凡拔尖的人竞选总统,现在票数已经统计完毕,请你算出谁能够当上总统. 输入输出 ...

  3. mysql 获取自增id的值的方法

    原生jdbc方式: Statement.getGeneratedKeys() 示例: Statement stmt = null; ResultSet rs = null; try { // // C ...

  4. python3中让程序暂停运行的语句

    https://blog.csdn.net/zmz971751504/article/details/78288988

  5. C# C++ 字符串传递

    C# C++ 字符串传递 标签: c#c++bytestring测试c 2012-06-14 17:425707人阅读评论(3)收藏举报 分类: C#(11)  作者同类文章X C++(112)  作 ...

  6. 【Codeforces Round #432 (Div. 2) A】 Arpa and a research in Mexican wave

    [链接]h在这里写链接 [题意] 在这里写题意 [题解] t<=k,输出t t>=n,输出k-t+n 其他情况都是k [错的次数] 0 [反思] 在这了写反思 [代码] /* */ #in ...

  7. 洛谷——U10223 Cx大帝远征埃及

    https://www.luogu.org/problem/show?pid=U10223#sub 题目背景 众所周知,Cx是一个宇宙大犇.Cx能文善武,一直在为大一统的实现而努力奋斗着.Cx将调用他 ...

  8. tensorflow compile

    bazel  build  --spawn_strategy=standalone tensorflow/examples/label_image/...

  9. 关于Altium Designer的BOM,元件清单

    在生成BOM列表的时候,要记得调整BOM的表格的宽度,以免显示不全, 还有就是BOM列表一共有 comment栏 ,description栏,designator栏,footprint栏,libref ...

  10. EL表达式.md

    操作符 描述 . 访问一个Bean属性或者一个映射条目 [] 访问一个数组或者链表的元素 ( ) 组织一个子表达式以改变优先级 + 加 - 减或负 * 乘 / or div 除 % or mod 取模 ...