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. Maybe someday FastCGI Cache will have an option to have the cache files be owned by the parent folder owner, but until then we need our cache daemon.

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. 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.

Ready to give SpinupWP a try?

Not sure if SpinupWP is the right fit for you? Send us an email at sudo@spinupwp.com and we’ll give you our no-BS opinion.