Using LUA directly in Nginx to access MySQL and memcaced to achieve a unified data interface

Source: Internet
Author: User
Tags curl lua memcached

Installation nginx See "Nginx+lua+redis Building high Concurrency Applications"


Let the nginx_lua_module in Nginx support MySQL and memcache
Download
Https://github.com/agentzh/lua-resty-memcached
Https://github.com/agentzh/lua-resty-mysql


There are many ways to handle the unification of access interfaces, which is described using Nginx lua to access MySQL and cache it with Memcache.

The configuration is as follows:

        ... 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 = R Equire "memcached" local memc, err = memcached:new () if not MEMC then NGX . Say ("Failed to instantiate MEMC:", err) return end Memc:set_timeout (100                    0)--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, E                    rr = 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--1 sec local OK, err, errno, SQLState = db: connect{host = "172.16.18.162", port = 3306, Databas E = "Test", user = "root", password = "Cpyf", Max_pac Ket_size = 1024x768 * 1024x768} if not ok then Ngx.say ("Failed to Co Nnect: ", 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 and 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, +) 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) '; }        ...

Second run:

Curl--get Http://app.ca-sim.com/getinfo?appleid=jfy

MySQL found[{"val": "123", "tag": "Jfy"}]


Run after the second time:

Curl--get Http://app.ca-sim.com/getinfo?appleid=jfy

MEMC found[{"val": "123", "tag": "Jfy"}]

The result has been cached


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.