The SpinupWP Cache Daemon

The cache daemon is a little Go app we’ve written and deploy to every server to help with purging the page cache. The reason we need this daemon is an issue of file permissions. Each site runs a PHP pool as the site user but since cache files are owned by user www-data PHP cannot delete the cache files.

The cache daemon listens on port 7836 for purge requests and deletes the requested path. Supervisor is used to ensure the cache daemon is always running, even after a server reboot. The daemon only listens on localhost, therefore it’s not accessible from outside the server.

Clearing the Cache

If you’re using our plugin, cache purging will be handled for you automatically when content changes. You will also have access to the following helper functions, which you can use from your own hooks:

// Purge the entire SpinupWP page cache.
spinupwp_purge_site()

// Purge a single post from the SpinupWP page cache.
spinupwp_purge_post( $post_id|$post )

// Purge a single URL from the SpinupWP page cache.
spinupwp_purge_url( $url )

The following WP-CLI commands are also available:

wp spinupwp cache purge-site
wp spinupwp cache purge-post <id>
wp spinupwp cache purge-url <url>

However, if you’re not running WordPress or do not wish to use our plugin you can manually make purge requests. A manual purge request can be made by sending a file path followed by a new line to port 7836. In PHP this might look something like this:

$path = '/cache/example.com';

$fp = @fsockopen( '127.0.0.1', '7836' );

if ( $fp ) {
    fwrite( $fp, $path . "\n" );

    $result = fread( $fp, 512 );
    fclose( $fp );

    return (bool) preg_match('/^Purge:\sSuccess/', $result);
}

The daemon will return Purge: Success\n or Purge: Error\n depending on whether the path could be deleted. If the path doesn’t exist, success will be reported. For security reasons, only paths within the /cache/ directory can be deleted.