Partial Audio , Video Caching

85 views
Skip to first unread message

Hadi Abbasi

unread,
May 16, 2021, 11:17:16 AMMay 16
to openresty-en
Hello
I'm using header_filter_by_lua & body_filter_by_lua  for caching!
I don't use nginx self rage -request caching system!
for caching audio & video using header & body filter systems, I can use this code for status 206 (partial content - range request):

body_filter_by_lua_block {
    local cache_possible = false;
    local response_content_data = nil;
    if ngx.arg[1] then --on body buffering
        ngx.ctx["resBody"] = ngx.ctx["resBody"] .. ngx.arg[1]
    end

    if ngx.arg[2] == true then  --response body is loaded completely
        response_content_data = ngx.ctx["resBody"]
        if ngx.status == 206 then
            if ngx.header["content-range"] ~= nil and tonumber(ngx.var.content_length) > 1 then
                    local start_range , end_range , total_range = string.match(ngx.header["content-range"], "bytes (%d+)-(%d+)/(%d+)")
                    if start_range ~= nil and end_range ~= nil and total_range ~= nil then
                        start_range = tonumber(start_range)
                        end_range = tonumber(end_range)
                        total_range = tonumber(total_range)
                        if start_range == 0 and tonumber(ngx.var.content_length) ==  total_range and end_range + 1 == total_range then
                            cache_possible =  true
                        end
                    end
            end
        else
            -- some codes
        end
        if cache_possible == true then
            save_meta_data(meta_data);
            save_body_data(response_content_data);
        end
    end
}


it works for me on caching & loading from cache times, but not always!
because at many states and times, I see caching is ignored and partial content is coming from origin server!
is it correct code?
if not, can I use filter system for caching partial contents?
if not, what is your suggestion? nginx self caching mechanism or (probably) openresty self mechanism?
Thanks a lot..
Best,
Hadi

Hadi Abbasi

unread,
May 16, 2021, 11:25:23 AMMay 16
to openresty-en
loading from cache is at other section & I've managed cache id at another section.
I wanna know if this code is correct for saving partial contents or there is a better way!?

Piotr Przybylski

unread,
May 24, 2021, 4:03:54 PMMay 24
to openresty-en
How are you saving your data? Where? Maybe it's a race condition, where two workers got the same request? Or the cache isn't shared between workers? This snippet isn't enough to tell what may be wrong.

Why aren't you using the slice module (http://nginx.org/en/docs/http/ngx_http_slice_module.html)? This would allow you to remove a lot of custom code.

Hadi Abbasi

unread,
May 30, 2021, 7:02:09 AMMay 30
to openresty-en

where: I have created a directory in my linux server that openresty is working on it so I can save all of my data in it!
how: I can save headers and body in separated files using io:write command! it's working properly because none-partial contents are OK by my caching method!
why: suppose that I wanna optimize / minify my caches audio / video files! so how can I do it by nginx builtin partial cache?

Piotr Przybylski

unread,
May 30, 2021, 5:39:52 PMMay 30
to openresty-en
I was thinking about using nginx's slice [1] directive. It will handle ranges transparently, and with proxy_cache_lock [2] you can ensure that there's only one concurrent quest going to your upstream server.

Reply all
Reply to author
Forward
Message has been deleted
0 new messages