Checking multiple caches before forwarding request to upstream

sachin.shetty@gmail.com nginx-forum at forum.nginx.org
Fri Apr 7 15:53:39 UTC 2017


Hi Maxim,

I found one way to make this work using lua to set the cache name. It seems
to be working ok, all my tests passed. 

""" Lua Script 
local resty_md5 = require "resty.md5"
local str = require "resty.string"
local md5 = resty_md5:new();
local posix = require("posix")
local days_30 = 1000 * 60 * 60 * 24 * 30
local days_90 = days_30 * 3

file_exists = function(path)
	file_stat = posix.stat(path);
	if file_stat == nil or file_stat.type ~= 'regular' then
		ngx.log(ngx.ERR, "file does not exists: ", path)
		return false
	end
	ngx.log(ngx.ERR, "file exists: ", path)
	return true;
end

pick_cache = function(last_modified)
	local now = ngx.now()
	if now - last_modified < days_30 then
		return "cache_recent"
	elseif now - last_modified < days_90 then
		return "cache_midterm"
	else
		return "cache_longterm"
	end
end



md5:update(ngx.var.request_uri)
local digest = md5:final()
local md5_str = str.to_hex(digest)
ngx.log(ngx.ERR, "md5: ", md5_str)

local cache_dirs = {
	"../cache_recent",
	"../cache_midterm",
	"../cache_longterm",
}

local file_name = string.sub(md5_str, -1) .. "/" .. string.sub(md5_str, -3,
-2)   .. "/" .. md5_str

for cache_count = 1, #cache_dirs do
	if file_exists(cache_dirs[cache_count] .. '/' .. file_name) then
		cache_name = string.gsub(cache_dirs[cache_count], ".*/", "")
		return cache_name
	end
end

--return pick_cache(ngx.now() - 10) -- from request header
--return pick_cache(ngx.now() - days_30) --  from request header
return pick_cache(ngx.now() - days_90) --  from request header
"""

Nginx conf:
set_by_lua_file $cache_name conf/cache_picker.lua;
proxy_cache $cache_name;

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,273446,273473#msg-273473



More information about the nginx mailing list