在nginx中使用lua直接访问mysql和memcaced达到数据接口的统一_MySQL
May 31, 2016 am 08:48 AMNginx
bitsCN.com安装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 --gethttp://app.ca-sim.com/getinfo?appleid=jfy
mysql found[{"val":"123","tag":"jfy"}]
로그인 후 복사
第二次后运行:
curl --gethttp://app.ca-sim.com/getinfo?appleid=jfy
memc found[{"val":"123","tag":"jfy"}]
로그인 후 복사
结果已被缓存
bitsCN.com본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

인기 기사
스플릿 소설을이기는 데 얼마나 걸립니까?
3 몇 주 전
By DDD
Repo : 팀원을 부활시키는 방법
3 몇 주 전
By 尊渡假赌尊渡假赌尊渡假赌
헬로 키티 아일랜드 어드벤처 : 거대한 씨앗을 얻는 방법
3 몇 주 전
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 에너지 결정과 그들이하는 일 (노란색 크리스탈)
1 몇 주 전
By 尊渡假赌尊渡假赌尊渡假赌

인기 기사
스플릿 소설을이기는 데 얼마나 걸립니까?
3 몇 주 전
By DDD
Repo : 팀원을 부활시키는 방법
3 몇 주 전
By 尊渡假赌尊渡假赌尊渡假赌
헬로 키티 아일랜드 어드벤처 : 거대한 씨앗을 얻는 방법
3 몇 주 전
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 에너지 결정과 그들이하는 일 (노란색 크리스탈)
1 몇 주 전
By 尊渡假赌尊渡假赌尊渡假赌

뜨거운 기사 태그

메모장++7.3.1
사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전
중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기
강력한 PHP 통합 개발 환경

드림위버 CS6
시각적 웹 개발 도구

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)

뜨거운 주제
Gmail 이메일의 로그인 입구는 어디에 있나요?
7280
9


자바 튜토리얼
1622
14


Cakephp 튜토리얼
1340
46


라라벨 튜토리얼
1257
25


PHP 튜토리얼
1205
29



Alter Table 문을 사용하여 MySQL에서 테이블을 어떻게 변경합니까?

MySQL의 문제를 해결하는 방법 공유 라이브러리를 열 수 없습니다.

Linux에서 MySQL을 실행합니다 (Phpmyadmin이있는 Podman 컨테이너가 포함되지 않음)

일반적인 취약점 (SQL 주입, 무차별 적 공격)에 대해 MySQL을 어떻게 보호합니까?
