Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why still get into srcache_store handler even if $srcache_expire=0? #45

Open
wonderbeyond opened this issue May 5, 2016 · 8 comments
Open

Comments

@wonderbeyond
Copy link

I just put my srcache settings directly under server block:

server {
    set $canonical_host www.example.com;

    # ...

    srcache_default_expire 0;
    set_escape_uri $pagecache_key $canonical_host$uri$is_args$args;
    srcache_fetch GET /pagecache key=$pagecache_key;
    srcache_store PUT /pagecache key=$pagecache_key&exptime=$srcache_expire;
    include srcache_status_headers;

    # ...
}

You see, I use srcache_default_expire, So the $srcache_expire will default to 0,
I expect my upstream setting their appropriate exptime by max-age.

However, my srcache_store get involved even if $srcache_expire==0,
and then cause an extra useless redis request.

location = /redis_put {
    # tested OK with SSDB
    internal;
    set_unescape_uri $exptime $arg_exptime;

    redis2_query setx $redis_key $echo_request_body $exptime;

    redis2_pass pagecache_ssdb;
}
@agentzh
Copy link
Member

agentzh commented May 5, 2016

@wonderbeyond I think you should use the srcache_store_skip directive for that. Zero exptime does not affect whether srcache_store is run.

@wonderbeyond
Copy link
Author

I known srcache_store_skip, but it's difficult for complex use case. Since it requires a variable, not a logic block(eg: lua code).
If I depend multiple variables to determine whether to skip, map directive is incapable.
Since some vars are only ready after upstream response, set_by_lua is alse hard.

@agentzh
Copy link
Member

agentzh commented May 6, 2016

@wonderbeyond You can always use access_by_lua* or rewrite_by_lua* to evaluate the value of your custom nginx variable for srcache_skip. Don't use map; its lazy evaluation can also lead to surprises.

@agentzh
Copy link
Member

agentzh commented May 6, 2016

@wonderbeyond Given that $srcache_exptime is not mandatory. The user may or may not use it for its srcache subrequests and ngx_srcache indeed has no control over that and it even has no idea about that. For this reason, I don't think it's a good idea for ngx_srcache to always assume $srcache_exptime is actually used and make srcache_store behave completely upon its value. The user may want to forcibly cache things by hard-coding a fixed exptime in its srcache_store request line, for example.

@wonderbeyond
Copy link
Author

@agentzh thank you, I've understood that $srcache_expire is only a convenience by srcache which respects upstream's Cache-Control.

I think it's perfect if my cache layer can interact with upstream, so I've made use of $srcache_expire.

You recommend me:

use access_by_lua* or rewrite_by_lua* to evaluate the value of of your custom nginx variable for srcache_skip.

However, since I need to combine factors from upstream response to evaluate the value, access_by_lua* and rewrite_by_lua* won't work. So what's the possible place?

@agentzh
Copy link
Member

agentzh commented May 7, 2016

@wonderbeyond It's possible with header_filter_by_lua* if the info is in the upstream response header. But be careful with output filter ordering (better use OpenResty to be sure).

@wonderbeyond
Copy link
Author

@agentzh Thanks, I will give a try.

I will set ngx.var.srcache_nostore=true in header_filter_by_lua_block{}, and expect srcache_store_skip $srcache_nostore works...

PS: I'm using original Nginx plused plugins coming from OpenResty.

@wonderbeyond
Copy link
Author

wonderbeyond commented Mar 30, 2017

So late I am back here again.


Below is my new configuration sample:

server {
    set $canonical_host www.example.com;

    # ...

    set_escape_uri $pagecache_key $canonical_host$uri$is_args$args;
    set $pagecache_exptime '';

    header_filter_by_lua_block {
        # MY DOUBT: this block runs in `output-header-filter` phase.
        # Can I ensure this block always run after `$srcache_expire` is evaluated???
        if not ngx.var.pagecache_exptime or ngx.var.pagecache_exptime == '' then
            ngx.var.pagecache_exptime = ngx.var.srcache_expire
        end
    }

    srcache_fetch GET /pagecache key=$pagecache_key;
    srcache_store PUT /pagecache key=$pagecache_key&exptime=$pagecache_exptime;

    location /foo {
        # I've set $pagecache_exptime, so I **dont respect upstream's Cache-Control**
        set $pagecache_exptime 1y;
    }

    location /bar {
        # I dont set `$pagecache_exptime` and **respect upstream's Cache-Control**
    }

    # ...
}

I leveraged the auto-evaluated $srcache_expire to determine my exptime, But a specific location can force a new exptime by setting $pagecache_exptime.

I compute my exptime in header_filter_by_lua_block, But Can I ensure this block always run after $srcache_expire is evaluated???

PS: I'm not using 100% OpenResty, but a self-built nginx using many components form OpenResty.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants