April 07, 2017 11:53AM
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;
Subject Author Posted

Checking multiple caches before forwarding request to upstream

sachin.shetty@gmail.com April 06, 2017 06:45AM

Re: Checking multiple caches before forwarding request to upstream

Maxim Dounin April 06, 2017 10:56AM

Re: Checking multiple caches before forwarding request to upstream

sachin.shetty@gmail.com April 06, 2017 11:04AM

Re: Checking multiple caches before forwarding request to upstream

sachin.shetty@gmail.com April 07, 2017 11:53AM



Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 273
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready