[root@localhost lua]# pwd /opt/openresty/nginx/lua [root@localhost lua]# cat mysql_test.lua local mysql = require "resty.mysql" localdb, err = mysql:new()
if not db then ngx.say("failed to instantiate mysql: ", err) return end
db:set_timeout(1000)
local ok, err, errno, sqlstate = db:connect{ host = "127.0.0.1", port = 3306, database = "ngx_lua", user = "root", password="digdeep", max_packet_size = 1024 * 1024 }
if not ok then ngx.say("failed to connect: ", err, ": ", errno, " ", sqlstate) return end
ngx.say("connected to mysql.")
local res, err, errno, sqlstate = db:query("drop table if exists cats") if not res then ngx.say("bad result: ", err, ": ", errno, ": ", sqlstate, ".") return end
res, err, errno, sqlstate = db:query("create table cats " .. "(id int not null primary key auto_increment, " .. "name varchar(30))") if not res then ngx.say("bad result: ", err, ": ", errno, ": ", sqlstate, ".") return end
ngx.say("table cats created.")
res, err, errno, sqlstate = db:query("insert into cats(name) " .. "values (\'Bob\'),(\'\'),(null)") if not res then ngx.say("bad request: ", err, ": ", errno, ": ", sqlstate, ".") return end
res, err, errno, sqlstate = db:query("select * from cats order by id asc", 10) if not res then ngx.say("bad result ", err, ": ", errno, ": ", sqlstate, ".") return end
local cjson = require "cjson" ngx.say("result: ", cjson.encode(res))
local ok, err = db:set_keepalive(1000, 100) if not ok then ngx.say("failed to set keepalive: ", err) return end
[root@localhost lua]# cat cache.lua local redis = require "resty.redis" local red = redis:new()
function set_to_cache(key, value, exptime) ifnot exptime then exptime = 0 end local ngx_cache = ngx.shared.ngx_cache local succ, err, forcible = ngx_cache:set(key, value, exptime) return succ end
function get_from_cache(key) local ngx_cache = ngx.shared.ngx_cache; localvalue = ngx_cache:get(key) ifnotvaluethen value = get_from_redis(key) set_to_cache(key, value) returnvalue end
ngx.say("get from cache.") returnvalue end
function get_from_redis(key) red:set_timeout(1000)
local ok, err = red:connect("127.0.0.1", 6379) ifnot ok then ngx.say("failed to connect: ", err) return end
local res, err = red:get(key) ifnot res then ngx.say("failed to get doy: ", err) return ngx.null end
ngx.say("get from redis.") return res end
function set_to_redis(key, value) red:set_timeout(1000) local ok, err = red:connect("127.0.0.1", 6379) ifnot ok then ngx.say("failed to connect: ", err) return end
local ok, err = red:set(key, value) ifnot ok then ngx.say("failed to set to redis: ", err) return end return ok end
set_to_redis('dog', "Bob") local rs = get_from_cache('dog') ngx.say(rs)
测试:
1 2 3 4 5 6 7 8 9
[root@localhost ~]# curl localhost/cache getfrom redis. Bob [root@localhost ~]# curl localhost/cache getfrom cache. Bob [root@localhost ~]# curl localhost/cache getfrom cache. Bob
[root@localhost lua]# cat cache_lock.lua local redis = require "resty.redis" local red = redis:new() local resty_lock = require "resty.lock" local ngx_cache = ngx.shared.ngx_cache
function set_to_cache(key, value, exptime) ifnot exptime then exptime = 0 end local succ, err, forcible = ngx_cache:set(key, value, exptime) return succ end
function get_from_cache(key) local ngx_cache = ngx.shared.ngx_cache; localvalue = ngx_cache:get(key) ifnotvaluethen-- cache miss locallock = resty_lock:new("cache_lock") local elapsed, err = lock:lock(key) ifnot elapsed then return fail("failed to acquire the lock: ", err) end
value = get_from_redis(key) ifnotvaluethen local ok, err = lock:unlock() ifnot ok then return fail("failed to unlock: ", err) end ngx.say("no value found") return end
local ok, err = ngx_cache:set(key, value, 1) ifnot ok then local ok, err = lock:unlock() ifnot ok then return fail("failed to unlock: ", err) end return faile("failed to update ngx_cache: ", err) end
local ok, err = lock:unlock() ifnot ok then return faile("failed to unlock: ", err) end
returnvalue end
ngx.say("get from cache.") returnvalue end
function get_from_redis(key) red:set_timeout(1000)
local ok, err = red:connect("127.0.0.1", 6379) ifnot ok then ngx.say("failed to connect: ", err) return end
local res, err = red:get(key) ifnot res then ngx.say("failed to get doy: ", err) return ngx.null end
ngx.say("get from redis.") return res end
function set_to_redis(key, value) red:set_timeout(1000) local ok, err = red:connect("127.0.0.1", 6379) ifnot ok then ngx.say("failed to connect: ", err) return end
local ok, err = red:set(key, value) ifnot ok then ngx.say("failed to set to redis: ", err) return end return ok end
set_to_redis('dog', "Bob") local rs = get_from_cache('dog') ngx.say(rs)
测试:
1 2 3 4 5 6
[root@localhost lua]# curl localhost/cache_lock getfrom cache. Bob [root@localhost lua]# curl localhost/cache_lock getfrom cache. Bob