Lua 是巴西里约热内卢天主教大学(Pontifical Catholic University of Rio de Janeiro)里的一个研究小组于 1993 年开发的,该小组成员有:Roberto Ierusalimschy、Waldemar Celes 和 Luiz Henrique de Figueiredo。
-- 导入redis模块 local redis = require("resty.redis") -- 定义释放redis连接的方法 localfunctionclose_redis(red) ifnot red then return end local ok, err = red:close() ifnot ok then ngx.say("close redis error : ", err) end end
--创建实例 local red = redis:new() --设置超时(毫秒) red:set_timeout(1000) --建立连接 ,这里要指定redis的安装的ip和端口 local ip = "127.0.0.1" local port = 6379 local ok, err = red:connect(ip, port) ifnot ok then ngx.say("connect to redis error : ", err) return close_redis(red) end
--调用API进行处理 ok, err = red:set("msg", "hello,"..ngx.var[1]) ifnot ok then ngx.say("set msg error : ", err) return close_redis(red) end --调用API获取数据 local resp, err = red:get("msg") ifnot resp then ngx.say("get msg error : ", err) return close_redis(red) end --得到的数据为空处理 if resp == ngx.null then resp = 'default'--比如默认值 end ngx.say("msg : ", resp) close_redis(red)
-- 导入模块 local common = require("common") local read_redis = common.read_redis local read_http = common.read_http local cjson = require("cjson") local template = require("resty.template") -- 常用变量和方法 local ngx_log = ngx.log local ngx_ERR = ngx.ERR local ngx_exit = ngx.exit local ngx_print = ngx.print local ngx_re_match = ngx.re.match
-- 获取商品id local spuId = ngx.var.spuId -- 获取spu local spuKey = "page:spu:id:"..spuId local spuInfoStr = read_redis("127.0.0.1", 6379, {spuKey}) ifnot spuInfoStr then ngx_log(ngx_ERR, "redis not found spu info, back to http, spuId : ", spuId) spuInfoStr = read_http("/spu/"..spuId, {}) end ifnot spuInfoStr then ngx_log(ngx_ERR, "http not found spuInfoStr info, spuId : ", spuId) return ngx_exit(404) end
-- 获取sku local skuKey = "page:sku:id:"..spuId local skuInfoStr = read_redis("127.0.0.1", 6379, {skuKey}) ifnot skuInfoStr then ngx_log(ngx_ERR, "redis not found sku info, back to http, spuId : ", spuId) skuInfoStr = read_http("/sku/"..spuId, {}) end ifnot skuInfoStr then ngx_log(ngx_ERR, "http not found skuInfoStr info, spuId : ", spuId) return ngx_exit(404) end -- 获取spuDetail local detailKey = "page:detail:id:"..spuId local detailInfoStr = read_redis("127.0.0.1", 6379, {detailKey}) ifnot detailInfoStr then ngx_log(ngx_ERR, "redis not found detail info, back to http, spuId : ", spuId) detailInfoStr = read_http("/detail/"..spuId, {}) end ifnot detailInfoStr then ngx_log(ngx_ERR, "http not found detailInfoStr info, spuId : ", spuId) return ngx_exit(404) end -- 获取categories local spuInfo = cjson.decode(spuInfoStr) local cid3 = spuInfo["categoryIds"][3] local categoryKey = "page:category:id:"..cid3 local categoryStr = read_redis("127.0.0.1", 6379, {categoryKey}) ifnot categoryStr then local idStr = table.concat(spuInfo["categoryIds"],","); ngx_log(ngx_ERR, "redis not found category info, back to http, categoryIds : ", idStr) categoryStr = read_http("/categories/", {ids = idStr}) end ifnot categoryStr then ngx_log(ngx_ERR, "http not found categoryStr info, categoryId : ", cid3) return ngx_exit(404) end -- 获取品牌 local brandId = spuInfo["brandId"] local brandKey = "page:brand:id:"..brandId local brandStr = read_redis("127.0.0.1", 6379, {brandKey}) ifnot brandStr then ngx_log(ngx_ERR, "redis not found brand info, back to http, brandId : ", brandId) brandStr = read_http("/brand/"..brandId, {}) end ifnot brandStr then ngx_log(ngx_ERR, "http not found brandStr info, brandId : ", brandId) return ngx_exit(404) end -- 获取规格 local specKey = "page:spec:id:"..cid3 local specStr = read_redis("127.0.0.1", 6379, {specKey}) ifnot specStr then ngx_log(ngx_ERR, "redis not found spec info, back to http, cid3 : ", cid3) specStr = read_http("/spec/"..cid3, {}) end ifnot specStr then ngx_log(ngx_ERR, "http not found specStr info, cid3 : ", cid3) return ngx_exit(404) end -- 组织数据 local context = { name = spuInfo["name"], skuList = skuInfoStr, detail = detailInfoStr, categories = categoryStr, brand = brandStr, specs = specStr } --渲染模板 template.render("item.html", context)
-- 导入redis模块 local redis = require("resty.redis") -- 配置商品的本地缓存 local local_cache = ngx.shared.item_local_cache -- 日志 local ngx_log = ngx.log local ngx_ERR = ngx.ERR -- 读取本地缓存 localfunctioncache_get(key) ifnot local_cache then returnnil end return local_cache:get(key) end -- 写入本地缓存 localfunctioncache_set(key, value) ifnot local_cache then returnnil end return local_cache:set(key, value, 10 * 60) --10分钟 end
localfunctionclose_redis(red) ifnot red then return end --释放连接(连接池实现) local pool_max_idle_time = 10000--毫秒 local pool_size = 100--连接池大小 local ok, err = red:set_keepalive(pool_max_idle_time, pool_size) ifnot ok then ngx_log(ngx_ERR, "set redis keepalive error : ", err) end end -- 查询本地缓存,没有则查询redis, ip和port是redis地址,key是查询的key localfunctionread_cache(ip, port, key) -- 尝试读本地缓存 local resp = cache_get(key) -- ngx_log(ngx_ERR, "debug local cache data : ", resp) ifnot resp then ngx_log(ngx_ERR, "read local cache fail , key", key) -- 获取一个redis连接 local red = redis:new() red:set_timeout(1000) local ok, err = red:connect(ip, port) ifnot ok then ngx_log(ngx_ERR, "connect to redis error : ", err) return close_redis(red) end -- 利用get查询 resp, err = red:get(key) end -- 查询失败处理 ifnot resp then ngx_log(ngx_ERR, "get redis content error : ", err) return close_redis(red) end --得到的数据为空处理 if resp == ngx.null then resp = nil end cache_set(key, resp) close_redis(red) return resp end -- 查询redis的方法 ip和port是redis地址,keys是查询的key,数组格式 localfunctionread_redis(ip, port, key) -- 获取一个连接 local red = redis:new() red:set_timeout(1000) local ok, err = red:connect(ip, port) ifnot ok then ngx_log(ngx_ERR, "connect to redis error : ", err) return close_redis(red) end local resp = nil -- 利用get查询 resp, err = red:get(key) -- 查询失败处理 ifnot resp then ngx_log(ngx_ERR, "get redis content error : ", err) return close_redis(red) end --得到的数据为空处理 if resp == ngx.null then resp = nil end close_redis(red) return resp end -- 查询http请求的方法,path是请求路径,args是参数,table格式 localfunctionread_http(path, args) -- 默认查询地址走 /backend/page/,内部转发到8083端口 local resp = ngx.location.capture("/backend/page"..path, { method = ngx.HTTP_GET, args = args }) -- 查询失败的处理 ifnot resp then ngx_log(ngx_ERR, "request error") return end -- 返回状态码不是200就报错 if resp.status ~= 200then ngx_log(ngx_ERR, "request error, status :", resp.status) return end return resp.body end -- 将方法导出 local _M = { read_redis = read_redis, read_cache = read_cache, read_http = read_http } return _M
-- 导入模块 local common = require("common") local read_redis = common.read_redis local read_http = common.read_http local read_cache = common.read_cache local cjson = require("cjson") local template = require("resty.template") -- 常用变量和方法 local ngx_log = ngx.log local ngx_ERR = ngx.ERR local ngx_exit = ngx.exit local ngx_print = ngx.print local ngx_re_match = ngx.re.match
-- 获取商品id local spuId = ngx.var.spuId -- 获取spu local spuKey = "page:spu:id:"..spuId local spuInfoStr = read_redis("127.0.0.1", 6379, spuKey) ifnot spuInfoStr then ngx_log(ngx_ERR, "redis not found spu info, back to http, spuId : ", spuId) spuInfoStr = read_http("/spu/"..spuId, {}) end ifnot spuInfoStr then ngx_log(ngx_ERR, "http not found spuInfoStr info, spuId : ", spuId) return ngx_exit(404) end
-- 获取sku local skuKey = "page:sku:id:"..spuId local skuInfoStr = read_redis("127.0.0.1", 6379, skuKey) ifnot skuInfoStr then ngx_log(ngx_ERR, "redis not found sku info, back to http, spuId : ", spuId) skuInfoStr = read_http("/sku/"..spuId, {}) end ifnot skuInfoStr then ngx_log(ngx_ERR, "http not found skuInfoStr info, spuId : ", spuId) return ngx_exit(404) end -- 获取spuDetail local detailKey = "page:detail:id:"..spuId local detailInfoStr = read_redis("127.0.0.1", 6379, detailKey) ifnot detailInfoStr then ngx_log(ngx_ERR, "redis not found detail info, back to http, spuId : ", spuId) detailInfoStr = read_http("/detail/"..spuId, {}) end ifnot detailInfoStr then ngx_log(ngx_ERR, "http not found detailInfoStr info, spuId : ", spuId) return ngx_exit(404) end -- 获取categories local spuInfo = cjson.decode(spuInfoStr) local cid3 = spuInfo["categoryIds"][3] local categoryKey = "page:category:id:"..cid3 local categoryStr = read_cache("127.0.0.1", 6379, categoryKey) ifnot categoryStr then local idStr = table.concat(spuInfo["categoryIds"],","); ngx_log(ngx_ERR, "redis not found category info, back to http, categoryIds : ", idStr) categoryStr = read_http("/categories/", {ids = idStr}) end ifnot categoryStr then ngx_log(ngx_ERR, "http not found categoryStr info, categoryId : ", cid3) return ngx_exit(404) end -- 获取品牌 local brandId = spuInfo["brandId"] local brandKey = "page:brand:id:"..brandId local brandStr = read_cache("127.0.0.1", 6379, brandKey) ifnot brandStr then ngx_log(ngx_ERR, "redis not found brand info, back to http, brandId : ", brandId) brandStr = read_http("/brand/"..brandId, {}) end ifnot brandStr then ngx_log(ngx_ERR, "http not found brandStr info, brandId : ", brandId) return ngx_exit(404) end -- 获取规格 local specKey = "page:spec:id:"..cid3 local specStr = read_cache("127.0.0.1", 6379, specKey) ifnot specStr then ngx_log(ngx_ERR, "redis not found spec info, back to http, cid3 : ", cid3) specStr = read_http("/spec/"..cid3, {}) end ifnot specStr then ngx_log(ngx_ERR, "http not found specStr info, cid3 : ", cid3) return ngx_exit(404) end -- 组织数据1 local context = { name = spuInfo["name"], skuList = skuInfoStr, detail = detailInfoStr, categories = categoryStr, brand = brandStr, specs = specStr } --渲染模板 1 template.render("item.html", context)
create user canal@'%' IDENTIFIED by 'canal'; GRANT SELECT, REPLICATION SLAVE, REPLICATION CLIENT,SUPER ON *.* TO 'canal'@'%' identified by 'canal'; FLUSH PRIVILEGES;