Clearing the Cache for Selected Resources in MODX Revolution

You probably know that if you’re using the CacheMaster extra, it will clear the cache for the resource you are currently working on when you save it. What if you need to clear the cache for a one or more other resources as well? For example, you might want to clear the cache for the Home Page whenever a resources is saved so that a cached getResources tag on the Home Page will get current information.

The answer is a plugin attached to the OnDocFormSave System Event.

MODX logo

The Code

[code language=”php”]

/* Comma-separated list of the resources you want to clear */
$docs = ‘1,12,23’;
$docIds = explode(‘,’, $docs);

foreach($docIds as $docId) {
$doc = $modx->getObject(‘modResource’, $docId);
$ctx = $doc->get(‘context_key’);
$ck = $doc->getCacheKey();
/* We need to replace the mgr context here */
$mgrCtx = $modx->context->get(‘key’);
$cKey = str_replace($mgrCtx, $ctx, $ck);

$modx->cacheManager->delete($cKey, array(
xPDO::OPT_CACHE_KEY => $modx->getOption(‘cache_resource_key’, null, ‘resource’),
xPDO::OPT_CACHE_HANDLER => $modx->getOption(‘cache_resource_handler’, null,
xPDO::OPT_CACHE_FORMAT => (integer)$modx->getOption(‘cache_resource_format’, null,
$modx->getOption(xPDO::OPT_CACHE_FORMAT, null, xPDOCacheManager::CACHE_PHP))

How it Works

The plugin code just loops through the resources and clears the cache for each one. If you only want to clear the Home Page, you could use this for the first line:

[code language=”php”]
$docs = $modx->getOption(‘site_start’);

Using the site_start system setting here means that the code will keep on working even if you switch to a different Home Page.

The code that directs the cacheManager to clear the cache for a specific resource is a little difficult to follow. That’s because it could be used in conjunction with a variety of third-party cache handlers. In the default MODX setup, it gets the documents cache key with $doc->getCacheKey, then tells cacheManager to delete the item with that key, sending along some parameters to control the action. In the default configuration, these just tell CacheManager the name, location, and format of the cache file and what to do with it (delete it).

This code needs a little explanation:

[code language=”php”]
$ctx = $doc->get(‘context_key’);
$ck = $doc->getCacheKey();
/* We need to replace the mgr context here */
$mgrCtx = $modx->context->get(‘key’);
$cKey = str_replace($mgrCtx, $ctx, $ck);


In the first line, we get the context that the resource is in by tapping its context_key field. Then, we ask the resource for its cache key. Most of the time, the cache key is just a partial path to the cache file. So far, so good, but there is a quirk in the getCacheKey() method. It’s meant to be called in the front end, where the current context will be the context of the resource and the context key (e.g., ‘web’) will be part of the path to the resource’s cache file.

When called in the MODX Manager, though, getCacheKey() uses the name of the current context instead (usually mgr). That makes the path to the cache file wrong until we substitute the resource’s actual context_key for the name of the Manager context in the string.

The Manager context doesn’t have to be called 'mgr', so to be on the safe side, we get it with $modx->context->get('key'). Then, we use str_replace() on the cache key to replace the key of the Manager Context with the context_key we got from the resource. Most of the time, this code will just be replacing mgr with web.

For more information on how to use MODX to create a web site, see my web site Bob’s Guides, or
better yet, buy my book: MODX: The Official Guide.

Looking for quality MODX Web Hosting? Look no further than <a
href=””>Arvixe Web Hosting!

Tags: , , , , , , , , | Posted under MODX, MODX | RSS 2.0

Author Spotlight

Bob Ray

Bob Ray is the author of MODX: The Official Guide and over 30 MODX add-on components. He hosts Bob's Guides, a source of valuable information for MODX users, and has been very active in the MODX Forums with over 19,000 posts.

Leave a Reply

Your email address will not be published. Required fields are marked *