Skip to content

Instantly share code, notes, and snippets.

@trepmal
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trepmal/4b8795bfd3e9648817a1 to your computer and use it in GitHub Desktop.
Save trepmal/4b8795bfd3e9648817a1 to your computer and use it in GitHub Desktop.
#!/usr/bin/php
<?php
/**
* This works for the following nginx configuration:
*
* fastcgi_cache_path [..] levels=1:2 [..];
* fastcgi_cache_key "$scheme://$host$request_uri";
*
* Make this file executable
* `chmod +x /path/to/this/file`
* and use like this
* `/path/to/this/file http://example.com/uri/`
*/
$url = isset( $argv[1] ) ? $argv[1] : '';
$url = trim( $url, '/' );
if ( empty( $url ) ) {
die('Must provide a URL');
}
$url .= '/';
purge( $url );
function purge( $url ) {
$frag = parse_url( $url );
// edit as needed according to the first value in {proxy|fastcgi}_cache_path
$cache_root = '/etc/nginx/cache/';
$query = isset( $frag['query'] ) ? "?{$frag['query']}" : '';
// edit as needed according to the value in {proxy|fastcgi}_cache_key
$hash = md5( "{$frag['scheme']}://{$frag['host']}{$frag['path']}{$query}" );
// edit as needed according to the levels value in {proxy|fastcgi}_cache_path
$hash_path = $cache_root . substr( $hash, -1 ) .'/'. substr( $hash, -3, 2 ) .'/'. $hash;
if ( ! is_file( $hash_path ) ) {
echo "No cache set [{$url}::{$hash_path}]\n";
return;
}
if ( unlink( $hash_path ) ) {
echo "Cache removed [{$url}::{$hash_path}]\n";
} else {
echo "Cache could not be removed [{$url}::{$hash_path}]\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment