简体   繁体   English

使用Varnish缓存动态页面

[英]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. 我正在尝试在Apache服务器上缓存一个页面,该页面运行一个仪表板,该页面显示通过Salesforce db通过PHP的API从Salesforce数据库获取的信息。

The dynamic is the following, 动态如下:

clients->apache/php/sqlqueries->Salesforce 客户端-> Apache / PHP / SQLQUERY-> Salesforce

The dashboard consists in tables that reload the PHP pages containing the object that queries salesforce. 仪表板包含一些表,这些表可重新加载包含查询salesforce对象的PHP页面。

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. 为了进行查询,table1.php包含一个查询对象和一个Salesforce API插件,该插件在每次重新加载和查询时都登录到Salesforce。

The login return a session Id that gets put in the session and reused until expires. 登录名返回一个会话ID,该会话ID将放入会话中并重复使用直到过期。

Now, there are say 30 users, 5 tables with 30 columns each, one for each user that display info so it can be shared. 现在,假设有30个用户,每个表有30个列的5个表,每个用户一个显示信息以便可以共享。

2 tables are refreshed every 30 minutes and 3 every minute. 每30分钟刷新2个表,每分钟刷新3个表。

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. 有一个会话处理程序会在一段时间后终止会话,但是如果他们在回家之前刷新了东西,它将再运行9个小时。

Salesforce administrator don't like me much at this point. 此时,Salesforce管理员不太喜欢我。

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. 另外,还可以避免必须在本地数据库中复制所有内容,并且从查询中读取的数据每分钟3次。

That is why I decided to give Varnish cache a chance. 这就是为什么我决定给Varnish缓存一个机会的原因。

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. 问题是我看到它没有将表内容从缓存中传递给客户端,看起来确实呈现了HTML部分,但是它不断从运行仪表板站点的后端服务器获取表内容。

~# 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, 这是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'. 我只是基本上试图缓存所有cookie以保留“ PHPSESSID”。 I also tried to cache POST and GET just in case but that didn't really do the trick either. 我还尝试缓存POST和GET,以防万一,但实际上也没有成功。

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? 关于如何使Varnish在这种情况下工作或如何通过其他方法减少查询的任何想法?

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. 评论中的人是对的,您不应为此使用Varnish,但如果您别无选择,请使用它。 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 . 您可以更改vcl_hash函数以将cookie用作缓存键的一部分,请在此处查看 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. 我相信您可以避免删除所有不希望用作键的cookie。 There is an example of that in this link . 链接中有一个示例。

Good luck. 祝好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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