Save Your MODX Cache with cacheguard

Jul 6, 2013

Websites are not static things - or at least they shouldn't be. A thriving site will have new content published on an ongoing basis. Not only does MODX allow you to produce content easily, but it also has a robust caching scheme to preserve page load speed when visitors are consuming that content. This all sounds pretty peachy...so what's the issue?

Due to potential dependencies between pages, or Resources, on your site, every time a Resource is edited or created, MODX clears the cache for all Resources. On sites with a lot of content and a lot of visitors, this can be a problem, and a difficult one to overcome because any solution will depend greatly on the specific requirements of the site and site owner.

MODX has some advanced caching mechanisms to enable custom caching strategies, but there's one area the other methods haven't addressed, until now...

A Conundrum

Often when you're writing in MODX, you're working with an unpublished Resource. You don't want people seeing the post before it's finished, but you want to save your work as you go. By default, the "Empty Cache" option is selected in the Resource Edit View, under the "Settings" tab. I'm willing to bet that you rarely even look under that tab ;)

Furthermore, that setting isn't stored in the database - it has to be manually disabled every time you hit "Save". MODX was designed this way because forgetting to clear the cache when you want it cleared has potentially greater drawbacks than the reverse. However, the flip side is that, even if you try to diligently uncheck that box every time it's prudent to do so, you will inevitably forget to do it.

This results in the cache being cleared frequently while you work, for all the Resources on the site, published and unpublished.

A Solution

Enter cacheguard. This ingenious little plugin by fellow MODXer JP DeVries, disables the "Empty Cache" option when you're editing an unpublished Resource, so when you hit "Save", you're also "saving" the cache from having to be regenerated.

It's smart enough to know if you're editing a published, live Resource, in which case the plugin doesn't fire. So it very specifically addresses the problem at hand, right? Well, there's more...

A Challenge

When you're working on a new post, you also want to preview it on the front-end, as a visitor would see it. That's why there's a handy "View" button. Well since the cache wasn't cleared when you hit "Save", when you subsequently preview the content you will see the outdated, cached version. *sigh*

A Win

Never fear, MODX is here! Lead Architect Jason Coward contributed a brilliant call to the MODX cache manager, whereby upon hitting "Save", cacheguard identifies the cache file for the Resource you're working on, and clears only that cache file. Now, when you preview your work-in-progress, you will see the most up-to-date progression, without ruining the cache party for everyone else.

Now go install cacheguard from Package Management and publish some stuff to your site. Your cache will be safe and sound :)