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