Quote from: dowhatnow at May 02, 2011, 12:27 PM
Lets say I am on server 1, I make a change and the page is cached. The physical cache file is going to live on server 1 correct? So, if I clear the site cache, I will clear the physical files on whichever server I happen to land on.
That’s a great question actually. We have dealt with this using a couple of plugins...
The first, which I attach to OnWebPageComplete, registers each server instance for 20 minutes (this was designed for a cloud deployment that would launch and kill new server instances on demand, but you can remove the expiration time to avoid the overhead of writing those messages every 20 minutes) and then looks for any messages with the command ’clearCache’ specified. The second, attached to the OnSiteRefresh event allows clearing the cache in the manager to register a ’clearCache’ message with all of the remote server ’instances’ that have been registered by the first plugin. You can take the idea and tailor it for your environment, but in our experience the best practice is to isolate one server instance for the manager (via subdomain configuration or similar) that is separate from your remote web nodes. This allows activity to take place on the manager instance in high volume, then changes only get pushed out when the Refresh site action is taken from the Admin menu.
You could also configure the cache to expire at regular intervals instead.
Here is an example
remotecommands plugin (NOTE this is for MODX 2.1, and 2.0 would be slightly different, using clearCache() instead of refresh()):
<?php
/* RemoteCommands plugin -- register with OnWebPageComplete event */
/* find any remote commands to execute from the master instance */
$instance = $_SERVER['SERVER_ADDR'];
if (!empty($instance) && $modx->getService('registry', 'registry.modRegistry')) {
$modx->registry->addRegister('remotes', 'registry.modDbRegister', array('directory' => 'remotes'));
$modx->registry->remotes->connect();
/* register this instance */
$modx->registry->remotes->subscribe("/distrib/instances/");
$modx->registry->remotes->send("/distrib/instances/", array($instance => true), array('expires' => time() + 1440));
/* find any valid command messages for this instance and act on them */
$modx->registry->remotes->subscribe("/distrib/commands/{$instance}/");
$commands = $modx->registry->remotes->read(array('poll_limit' => 1, 'msg_limit' => 1));
if (!empty($commands)) {
$command = reset($commands);
if (!empty($command)) {
switch ($command) {
case 'clearCache':
$results= $modx->cacheManager->refresh();
break;
default:
break;
}
}
}
}
?>
And here is an example sendclearcache plugin for registering a remote command message to each remote server instance:
<?php
/* SendClearCache plugin -- register with OnSiteRefresh event */
/* read instances and write clear cache msg to each command directory */
if ($modx->getService('registry', 'registry.modRegistry')) {
$modx->registry->addRegister('remotes', 'registry.modDbRegister', array('directory' => 'remotes'));
$modx->registry->remotes->connect();
$modx->registry->remotes->subscribe('/distrib/instances/');
$instances = $modx->registry->remotes->read(array('poll_limit' => 1, 'msg_limit' => 25, 'remove_read' => false));
if (!empty($instances)) {
foreach ($instances as $instance) {
if ($instance == $_SERVER['SERVER_ADDR']) continue;
$modx->registry->remotes->subscribe("/distrib/commands/{$instance}/");
$modx->registry->remotes->send("/distrib/commands/{$instance}/", 'clearCache', array('expires' => time() + 1440));
}
}
}
?>