Openresty notes-access to memcache and MySQL

Source: Internet
Author: User
Tags php tutorial
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 ("

Hello, world

") '; } location/memcache_test {default_type Text/plain;content_by_lua '--argslocal args = Ngx.req.get_uri_args () if (arg s["key"] = = nil) thenreturnend--newlocal memcached = require "resty.memcached" local memc, err = memcached:new () if not mem C then Ngx.say ("Faild to init MEMC:", err) returnendmemc:set_timeout (+)--connectlocal OK, err = Memc:connect ("127.0.0. 1 ", 11211) if not OK and Ngx.say (" Faild to connect: ", err) returnend--getlocal res, flags, err = Memc:get (args[" key "]) I F Err Thenngx.say ("Faild to get:", err) returnendif res thenngx.say ("Get key ["]. args["Key"]. "] " .. " [" .. Res.. "]") Elsengx.say ("Not get key ["]. args["Key"]. "]") end--setlocal OK, err = Memc:set (args["key"], args["key"]) if OK Thenngx.say ("set OK") end '; }location/mysql_test{default_type Text/plain;content_by_lua ' local mysql = require "resty.mysql"--newlocal db, err = Mys Ql:new () if not db then Ngx.say ("Faild to init MySQL:", err) returnenddb:set_timeout (1000)--connectlocal OK, err, errno, sqlstate = Db:connect{host = "127.0.0.1", port = 3306,database = "Test", user = "root", pass Word = "123456", max_packet_size = 1024x768 * 1024}if not OK Thenngx.say ("Failed to connect to MySQL:", err, ":", errno, "" " , SQLState) Returnendngx.say ("connected to MySQL")--create tablelocal sql = [CREATE table if not EXISTS ' test ' (' id ' int (11 ) primary key NOT NULL auto_increment, ' name ' varchar (+) NOT null default \ ' \ ') engine=innodb default charset=utf8]]local Res, err, errno, sqlstate = db:query (SQL) if not res thenngx.say ("CREATE TABLE Error:", err, ":", errno, ":", Sqlsta Te, ".") Returnend--insertsql = "INSERT INTO ' test ' (' name ') VALUES (\ ' fish\ ')" Local res, err, errno, sqlstate = db:query (sql) if Not res Thenngx.say ("Insert error:", err, ":", errno, ":", SQLState, ".") Returnend--selectsql = "SELECT * from ' Test '" local res, err, errno, sqlstate = db:query (SQL) if not res Thenngx.say ("Sel ECT error: ", err,": ", errno,": ", SQLState,". ") ReturnendLocal Cjson = require "Cjson" Ngx.say (Cjson.encode (res))--deletesql = "Delete from ' Test '" local res, err, errno, Sqlstat e = db:query (SQL) if not res thenngx.say ("Delete error:", err, ":", errno, ":", SQLState, ".") Returnend ';} }}

The above describes the Openresty notes-access to memcache and MySQL, including aspects of the content, I hope to be interested in PHP tutorial friends helpful.

  • 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.