openresty笔记-访问memcache和mysql

WBOY
Lepaskan: 2016-08-08 09:32:00
asal
1081 orang telah melayarinya
worker_processes  1;                                                                                                                         
error_log logs/error.log;
events {
    worker_connections 1024;
}
http {
    server {
        listen 8080;
		
        location /hello_world {
            default_type text/html;
            content_by_lua '
                ngx.say("<p>hello, world</p>")
            ';
        }

        location /memcache_test {
			default_type text/plain;
			content_by_lua '
				--args
				local args = ngx.req.get_uri_args()
				if (args["key"] == nil) then
					return
				end
				--new
				local memcached = require "resty.memcached"
				local memc , err = memcached:new()
				if not memc then 
					ngx.say("faild to init memc:" , err)
					return
				end
				
				memc:set_timeout(1000)
				--connect
				local ok , err = memc:connect("127.0.0.1" , 11211)
				if not ok then 
					ngx.say("faild to connect:" , err)
					return
				end
				--get
				local res , flags , err = memc:get(args["key"])
				if err then
					ngx.say("faild to get: " , err)
					return
				end
				
				if res then
					ngx.say("get key [" .. args["key"] .. "] " .. "[" .. res .. "]")
				else
					ngx.say("not get key [" .. args["key"] .. "]")
				end
				
				--set
				local ok , err = memc:set(args["key"] , args["key"])
				if ok then
					ngx.say("set ok")
				end
			';
        }
		
		location /mysql_test{
			default_type text/plain;
			content_by_lua '
				local mysql = require "resty.mysql"
				--new
				local db , err = mysql:new()
				if not db then 
					ngx.say("faild to init mysql: " , err)
					return
				end
				
				db:set_timeout(1000)
				
				--connect
				local ok , err , errno , sqlstate = db:connect{
					host = "127.0.0.1",
					port = 3306,
					database = "test",
					user = "root",
					password = "123456",
					max_packet_size = 1024 * 1024
				}
				
				if not ok then
					ngx.say("failed to connect to mysql : ", err, ": ", errno, " ", sqlstate)
					return
				end
				
				ngx.say("connected to mysql")
				
				--create table
				local sql = [[create table if not exists `test`(
					`id` int(11) primary key not null auto_increment,
					`name` varchar(100) not null default \'\'
				) engine=Innodb default charset=utf8]]
				
				local res , err , errno , sqlstate = db:query(sql)
				
				if not res then
					ngx.say("create table error: ", err, ": ", errno, ": ", sqlstate, ".")
					return
				end
				
				--insert
				sql = "insert into `test` (`name`) values (\'fish\')"
				local res , err , errno , sqlstate = db:query(sql)
				if not res then
					ngx.say("insert error: ", err, ": ", errno, ": ", sqlstate, ".")
					return
				end
				
				--select
				sql = "select * from `test`"
				local res , err , errno , sqlstate = db:query(sql)
				if not res then
					ngx.say("select error: ", err, ": ", errno, ": ", sqlstate, ".")
					return
				end
				
				local cjson = require "cjson"
				ngx.say(cjson.encode(res))
				
				--delete
				sql = "delete from `test`"
				local res , err , errno , sqlstate = db:query(sql)
				if not res then
					ngx.say("delete error: ", err, ": ", errno, ": ", sqlstate, ".")
					return
				end
			';
		}
    }
}
Salin selepas log masuk

以上就介绍了openresty笔记-访问memcache和mysql,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan
Tentang kita Penafian Sitemap
Laman web PHP Cina:Latihan PHP dalam talian kebajikan awam,Bantu pelajar PHP berkembang dengan cepat!