在nginx中使用lua直接访问mysql和memcaced达到数据接口的统一

本文介绍了如何通过在nginx中使用lua_resty_memcached和lua_resty_mysql模块来实现对MySQL数据库的访问,并结合Memcached进行数据缓存,从而达到数据接口的统一。配置示例中展示了如何处理GET请求,根据参数从Memcached获取数据,若无数据则查询MySQL并存储到Memcached,确保后续请求能快速响应。
摘要由CSDN通过智能技术生成

让nginx 中的nginx_lua_module支持MySQL 和memcache
下载
https://github.com/agentzh/lua-resty-memcached
https://github.com/agentzh/lua-resty-mysql


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

配置如下:

[plain]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. ...  
  2. location /getinfo {  
  3.     default_type 'text/plain';  
  4.     content_by_lua '  
  5.   
  6.         local args = ngx.req.get_uri_args()  
  7.   
  8.         if args["appleid"] == nil then  
  9.             ngx.say("param appleid is nil")  
  10.             return  
  11.         end  
  12.   
  13.         local memcached = require "memcached"  
  14.         local memc, err = memcached:new()  
  15.         if not memc then  
  16.             ngx.say("failed to instantiate memc: ", err)  
  17.             return  
  18.         end  
  19.   
  20.   
  21.         memc:set_timeout(1000) -- 1 sec  
  22.   
  23.         local ok, err = memc:connect("172.16.18.114", 11211)  
  24.         if not ok then  
  25.             ngx.say("failed to connect: ", err)  
  26.             return  
  27.         end  
  28.   
  29.         local res, flags, err = memc:get(args["appleid"])  
  30.         if err then  
  31.             ngx.say("failed to get memc: ", err)  
  32.             return  
  33.         end  
  34.   
  35.         if not res then  
  36.   
  37.             local mysql = require "mysql"  
  38.             local db, err = mysql:new()  
  39.             if not db then  
  40.                 ngx.say("failed to instantiate mysql: ", err)  
  41.                 return  
  42.             end  
  43.   
  44.             db:set_timeout(1000) -- 1 sec  
  45.   
  46.             local ok, err, errno, sqlstate = db:connect{  
  47.                 host = "172.16.18.162",  
  48.                 port = 3306,  
  49.                 database = "test",  
  50.                 user = "root",  
  51.                 password = "cpyf",  
  52.                 max_packet_size = 1024 * 1024  
  53.             }  
  54.   
  55.             if not ok then  
  56.                 ngx.say("failed to connect: ", err, ": ", errno, " ", sqlstate)  
  57.                 return  
  58.             end  
  59.   
  60.             -- ngx.say("connected to mysql.")  
  61.   
  62.             sql = "select * from tagval where tag = \'" .. args["appleid"] .. "\'"  
  63.   
  64.             res, err, errno, sqlstate = db:query(sql)  
  65.             if not res then  
  66.                 ngx.say("bad result: ", err, ": ", errno, ": ", sqlstate, ".")  
  67.                 return  
  68.             end  
  69.   
  70.             local json = require "json"  
  71.             ngx.say("mysql found")  
  72.             ngx.say(json.encode(res))  
  73.   
  74.             local ok, err = memc:set(args["appleid"], json.encode(res))  
  75.             if not ok then  
  76.                 ngx.say("failed to set memc: ", err)  
  77.                 return  
  78.             end  
  79.   
  80.             local ok, err = db:set_keepalive(0, 100)  
  81.             if not ok then  
  82.                 ngx.say("failed to set keepalive: ", err)  
  83.                 return  
  84.             end  
  85.   
  86.             return  
  87.   
  88.         end  
  89.   
  90.         ngx.say("memc found")  
  91.         ngx.say(res)  
  92.   
  93.         memc:set_keepalive(0, 100)  
  94.   
  95.     ';  
  96. }  
  97. ...  

第二次运行:

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

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


第二次后运行:

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

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

结果已被缓存

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值