安装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. ArcGIS 10 影像、栅格数据格式批量转换

    转自原文 ArcGIS 10 影像.栅格数据格式批量转换 在做三维场景的时候,经常会涉及多种不同格式DEM数据或者影像的转换,如ASCII.GRID.IMG.TIFF等等,遇到大数据量时,我们就需要批 ...

  2. UVa 11015 - 05-2 Rendezvous

    題目:有一個班級的學生要一起寫作業,所以他們要到一個統一的地點.現在給你他們各自的位置, 問集合地點定在哪,能够讓全部人走的總路徑長度最小. 分析:圖論.最短路.直接利用Floyd計算最短路,找到和值 ...

  3. js进阶 14-9 ajax事件有哪些

    js进阶 14-9 ajax事件有哪些 一.总结 一句话总结:ajax开始时事件.发送时事件,请求完成时事件,请求成功时事件,请求结束时事件,请求错误时事件事件. 1.ajax事件的监听对象是谁? 都 ...

  4. web service 原理

    Web Service基本概念 Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intranet上的其它系统中传递过来的请求,轻量级的 ...

  5. jquery中$(dom).each()和$(dom).length的使用

    1.$(dom).each();在dom处理上用的比较多. $(selector).each(function(index,element){ //selector会遍历当前页面里所有匹配的jquer ...

  6. 【2017中国大学生程序设计竞赛 - 网络选拔赛 hdu 6150】Vertex Cover

    [链接]点击打开链接 [题意] 有人写了一个最小点覆盖的贪心算法,然后,让你去hack它. 并且,要求这个算法得到的错误答案,是正确答案的三倍. 让你任意输出hack数据,点数<=500 [题解 ...

  7. nginx+tomcat 架构 HttpServletRequest.getScheme()获取正确的协议

    http://blog.csdn.net/ofofw/article/details/46791447

  8. 使用Intent的Flag设置启动參数

    Intent中关于激活Activity的Flag Intent类定义了一批常量,用于配置激活Activity时的相关參数; 在Intent中设置Flag ·调用Intent的setFlags()或ad ...

  9. DIV+CSS学习笔记

    第十五章 定位 static静态定位(不对它的位置进行改变,在哪里就在那里) 默认值.没有定位,元素出现在正常的流中(忽略 top, bottom, left, right 或者 z-index 声明 ...

  10. DC中检查脚本错误

    dcprocheck    +     要检查的tcl文件