Created
February 26, 2014 07:18
-
-
Save wokamoto/9225119 to your computer and use it in GitHub Desktop.
[WordPress] 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 ) { | |
if ( !$url ) | |
return; | |
$params = array( | |
'@ktai', | |
'@smartphone', | |
'@smartphone.off', | |
); | |
foreach ( $params as $param ) { | |
NginxCachePurge::purge($url.$param); | |
} | |
} | |
add_action('nginxchampuru_flush_cache', 'nginx_flush_cache'); | |
Class NginxCachePurge { | |
const CACHE_LEVELS = '1:2'; | |
const CACHE_DIR = '/var/cache/nginx/proxy_cache'; | |
public static function purge($url) { | |
$cache_key = self::get_cache_key($url); | |
$cache = self::get_cache_file($cache_key); | |
if ( file_exists($cache) && @unlink($cache) ) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
private static function get_cache_key($url) { | |
return md5($url); | |
} | |
private static function get_cache_file($key) { | |
$levels = get_option('nginxchampuru-cache_levels'); | |
$levels = preg_split("/:/", $levels ? $levels : self::CACHE_LEVELS); | |
$cache_dir = get_option('nginxchampuru-cache_dir'); | |
$path = array(); | |
$path[] = $cache_dir ? $cache_dir : self::CACHE_DIR; | |
$offset = 0; | |
foreach ($levels as $l) { | |
$offset = $offset + $l; | |
$path[] = substr($key, 0-$offset, $l); | |
} | |
$path[] = $key; | |
return join("/", $path); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment