Varnish可以使用ESI标签为您的页面上的多个部分提供不同的缓存时间。
使用清漆 4
例如:index.php
<html>...
<?php
// Cached as normal in Varnish ( 120 seconds)
echo " Date 1 (cached): ".date('Y/m/d H:i:s') ."\n";
?>
<esi:include src="inc/sidebar.php"/>
<esi:remove>
<?php include('inc/sidebar.php'); ?>
<p>Not ESI!</p>
</esi:remove>
的sidebar.php
<?php
echo "<br>NOT CACHED : ".date('Y/m/d H:i:s');
?>
Config Varnish(default.vcl)
sub vcl_backend_response {
set beresp.do_esi = true;
## if sidebar remove cache else 120 seconds cache
if (bereq.url ~ "sidebar.php") {
## set beresp.ttl = 0s;
set beresp.uncacheable = true;
return(deliver);
} else {
set beresp.ttl = 120s;
}
以上工作正常,但我很想找到一种方法来控制我的项目。 如果我得到很多ESI文件,这个配置文件会变得很大。
我想到了这个:
首先在 index.php中:
<?php
header('Cache-Control: max-age=120');
?>
sidebar.php 中的
<?php
header('Cache-Control: max-age=0');
echo "<br>NOT CACHED : ".date('Y/m/d H:i:s');
?>
在config(default.vcl)
中import std;
sub vcl_backend_response {
set beresp.do_esi = true;
# Set total cache time from cache control or 120 seconds.
set beresp.ttl = std.duration(regsub(beresp.http.Cache-Control, ".*max-age=([0-9]+).*", "\1") + "s", 120s);
## Debug , shows up in header.
set beresp.http.ttl =beresp.ttl;
}
现在这部分工作..如果我只在index.php中放入标题max-age,它将控制此页面缓存的时间。 最大的问题是sidebar.php(max-age = 0)会覆盖所有内容,因此整个页面都不会被缓存。
有没有人知道解决方案..重要的是从php脚本中控制缓存时间(beresp.ttl)。