local uri=ngx.var.uri;
local res;
local cjson=require("cjson");
ngx.header.content_type="text/html";
if string.sub(uri,-3) == ".js" then
ngx.header.content_type="application/x-javascript; charset=utf-8";
else
ngx.header.content_type="text/css";
end
local split = function(s, p)
local rt= {}
string.gsub(s, '[^'..p..']+', function(w) table.insert(rt,{ w}) end )
return rt
end
--local ret = ngx.location.capture("/memcmd?cmd=get&key=".. ngx.var.key);
local ret={status=1};--注释上面这行用于调试开发,直接合并,不从memcache读。发布时候注释这行
if ret.status == 200 then
res=ret.body or "";
else
local list=split(ngx.var.query_string,"&");
local res1,res2,res3,res4,res5,res6 =ngx.location.capture_multi(list);
if res1.status==200 then
res=res1.body.."\n"..(res2 and res2.body or '').."\n"..(res3 and res3.body or '').."\n"..(res4 and res4.body or '').."\n"..(res5 and res5.body or '').."\n"..(res6 and res6.body or '');
ngx.location.capture("/memcmd?cmd=set&key=".. ngx.var.key .."&val=".. ngx.escape_uri(res));
else
res='error'..cjson.encode(res1);
end
end
ngx.say(res);
可以动态压缩合并,然后用memcache缓存。这样更新的时候删除memcache就好了。
给个lua用于合并的脚本。压缩功能网上很多。我这里是已经压缩好的。