一个简单的解决方案是在单击“清空所有缓存”按钮时使用$_GET
像这样:
function wpse_16722_purge_varnish() {
// Is the button submitted, Get the value
$purge = isset( $_GET[\'w3tc_note\'] ) ? trim( $_GET[\'w3tc_note\'] ) : \'\';
// Security-check, user can edit settings
// And the button "Empty all cache" is submitted
if( current_user_can(\'manage_options\') && $purge == \'flush_all\' ) {
// Maybe there is a better way to clear the cache
// in varnish, i found this on php.net
// Change this to match your Varnish-setup
$fp = fsockopen( "127.0.0.1", "80", $errno, $errstr, 2 );
if ( ! $fp ) {
echo = "$errstr ($errno)<br />\\n";
} else {
$out = "PURGE /alain HTTP/1.0\\r\\n";
$out .= "Host: giantdorks.org\\r\\n";
$out .= "Connection: Close\\r\\n\\r\\n";
fwrite( $fp, $out );
while ( ! feof( $fp ) ) {
echo fgets( $fp, 128 );
}
fclose( $fp );
}
}
}
add_action(\'admin_head\', \'wpse_16722_purge_varnish\');
Varnish配置语言(VCL)还具有url清除功能。可以通过purge\\u url(url\\u pattern)函数访问它。
acl purge_acl {
"localhost";
"some.hostname.ext";
"154.120.2.33";
}
sub vcl_recv {
if(req.request == "flush_all") {
if(!client.ip ~ purge_acl) {
error 405 "Not allowed";
} else {
purge_url(req.url);
error 200 "Purged";
}
}
}
上面的脚本具有请求方法(如GET&;的正常代理/缓存行为);邮递但当用户通过“flush\\u all”方法连接时,页面将被清除。