MODX Quick Tip: Cache Refresh Events

Jun 9, 2016

Why Do I Need To Read This?

If you're writing a plugin that performs some action when your MODX site cache is refreshed, then you'll likely benefit from knowing that there are actually two very useful MODX Events, for which you can "listen".


This is the obvious one. It's invoked when someone uses the "Clear Cache" main menu option in the Manager. The vast majority of times I've written cache management plugins, it's this Event I've enabled in the Plugin "Events" tab. However, this Event is not invoked when the cache is refreshed programmatically via the $modx->cacheManager->refresh() method. In fact, prior to version 2.3.3 , no Event was invoked to signal that method call. Yet another reason to update, if you're using an older version of MODX ;)


Thanks to MODX Community member argnist who submitted the PR, and MODX Chief Architect Jason Coward who merged it, the $modx->cacheManager->refresh() method invokes the "OnCacheUpdate" Event. The method is called frequently from code that needs to refresh the MODX cache, so really this should be the de-facto Event to listen for, if you want your code to execute on programmatic cache refreshes. This Event can be especially handy for third-party caching plugins—those that sync with CDN APIs, for example.

To use it, enable the "OnCacheUpdate" Event for your Plugin and call the cache refresh/clear method in your API.

Note: the "Clear Cache" menu action also calls the $modx->cacheManager->refresh() method, so if you listen for "OnCacheUpdate" you probably don't need to listen for "OnSiteRefresh" unless you need to know specifically when a User has manually performed the action.

Hope this helps in your MODX caching integrations!