Created
October 30, 2012 04:03
-
-
Save wokamoto/3978224 to your computer and use it in GitHub Desktop.
Nginx Cache Controller で複数サーバのキャッシュ消す奴
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function nginx_flush_cache( $url ) { | |
global $nginx_servers; | |
static $urls = array(); | |
if ( !$url ) | |
return; | |
if ( isset($urls[md5($url)]) ) | |
return; | |
$urls[md5($url)] = $url; | |
$log_file = '/tmp/nginx_flush_cache.log'; | |
if (!isset($nginx_servers)) { | |
$nginx_servers = array( | |
'192.168.0.1', | |
'192.168.0.2', | |
); | |
} | |
foreach ( $nginx_servers as $server ) { | |
$response = wp_remote_get('http://'.$server.'/purge/?url=' . rawurlencode($url)); | |
if( !is_wp_error( $response ) && $response["response"]["code"] === 200 ) { | |
// 成功時の処理 | |
//file_put_contents($log_file, "{$server} : {$url}\n", FILE_APPEND); | |
} else { | |
// エラー時の処理 | |
file_put_contents($log_file, "{$server}\n", FILE_APPEND); | |
} | |
} | |
} | |
add_action('nginxchampuru_flush_cache', 'nginx_flush_cache'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment