Created
August 29, 2012 15:21
-
-
Save wokamoto/3514268 to your computer and use it in GitHub Desktop.
簡易版 Nginx Cache Purge スクリプト
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 | |
if ( isset($_GET['url']) ) { | |
$url = stripslashes($_GET['url']); | |
NginxCachePurge::purge($url); | |
} | |
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) ) { | |
printf('Success purge : %s (%s)', $url, $cache_key) . "\n"; | |
} else { | |
printf('Failure purge : %s (%s)', $url, $cache_key) . "\n"; | |
} | |
} | |
private static function get_cache_key($url) { | |
return md5($url); | |
} | |
private static function get_cache_file($key) { | |
$levels = preg_split("/:/", self::CACHE_LEVELS); | |
$path = array(); | |
$path[] = 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