简体   繁体   中英

Cache dynamic page with Varnish

I am trying to cache a page that runs a dashboard displaying information pulled from a Salesforce db through APIs with PHP, on an Apache server.

The dynamic is the following,

clients->apache/php/sqlqueries->Salesforce

The dashboard consists in tables that reload the PHP pages containing the object that queries salesforce.

function refreshtable1() {
    $('#table1').load('/tables/table1.php', function() {
        setTimeout(refreshtable1, 60000);
    });
}

For that queries to happens, the table1.php includes a query object and a salesforce API plugin that login in to Salesforce on every reload and queries.

The login return a session Id that gets put in the session and reused until expires.

Now, there are say 30 users, 5 tables with 30 columns each, one for each user that display info so it can be shared.

2 tables are refreshed every 30 minutes and 3 every minute.

If you do the numbers that could go up to 1 million queries everyday as this guys keep leaving their browsers on. There is a session handler that kills the session after some time, but if they refresh the thing before going home, it runs for another 9 hours.

Salesforce administrator don't like me much at this point.

So I am trying to put some sort of system or cache to off load. Also to avoid to have to have a local db where I copy all and the read from the queries to populate are 3 per minute.

That is why I decided to give Varnish cache a chance.

Problem is that I see that it does not deliver the tables content to clients from the cache, it does look like the HTML part is rendered but the table content it keeps getting fetched from the backend server where the dashboard site run.

~# varnishstat -1 | grep "cache_hit \|cache_miss \|cache_hitpass"
MAIN.cache_hit                44         0.00 Cache hits
MAIN.cache_hitpass             0         0.00 Cache hits for pass.
MAIN.cache_miss            16436         0.59 Cache misses

I'm not sure if that is even possible so I would appreciate any advise.

This is the Varnish conf,

sub vcl_recv {

    if (!(req.http.host == "test.local")) {
        unset req.http.Cookie;
    }

}

I'm just basically trying to cache all cookies to keep 'PHPSESSID'. I also tried to cache POST and GET just in case but that didn't really do the trick either.

if (!(req.method  ~ "GET") && !(req.method  ~ "POST")) {
    return (pass);
}

Any ideas on how to get Varnish to work on this scenario or on how to reduce the querying with another method?

Cheers.

The people on the comments are right that you should not use Varnish for that, but if you don't have any choice go for it. I have used it as workaround of organization problems too.

You can change the vcl_hash function to use the cookies as a part of the cache key, look at it here . For example:

sub vcl_hash {
  hash_data(req.http.cookie);
}

You should pay attention to hash colisions . I believe that you can avoid it removing all the cookies which are not the ones that you want to use as key. There is an example of that in this link .

Good luck.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM