Published on July 9, 2013
If Cache is King...
In the world of website performance, Cache is King. Well, actually optimizing your codebase should come first, but assuming you've done that, the preeminent methods for increasing site speed all involve caching at some level. In this post we're going to explore the robust Add-on Packages in the MODX Extras Repo that can help you implement a finely-tuned, custom caching strategy.
Before we dive into the Add-on Packages, let's quickly review what MODX's core caching mechanism does, and how it does it:
- By default, writes cache files to the local filesystem under the /core/cache/ folder in various partitions
- Caches the output of any cacheable Resource in the resource cache partition, on first request
- Clears the cache when a Resource is saved, or created - unless the "Empty Cache" option is manually disabled
getCache allows you to store the output of specific Snippet calls and Chunk calls from your template into a custom folder location that you specify, which can optionally persist even after the Resource cache is cleared.
Use case example: you have global elements like navigation, footers, etc that don't need to be refreshed every time a new Resource is published. These elements may include slower queries, etc that can beneifit from being cached on a more persistent basis. getCache lets you do all that and more.
statcache renders fully cacheable resources into flat, static files. A server directive is required to try the statcache location on the filesystem first, and only if the static file does not exist will MODX then handle the request.
Use case example: you have certain pages that don't contain any dynamic content, such as an "about" page, but receive a lot of traffic. By circumventing php entirely for those requests, your server can deliver more responses with less processor load.
microcache sets public cache-control headers on cacheable Resources so that browsers and upstream proxies can cache responses, reducing load on your server.
Use case example: pretty much every MODX site can benefit from this. The plugin is smart enough to know when a Resource isn't cacheable, like a session-dependent one, so you just install the plugin and watch the magic happen.
URL in Extras Repo: http://modx.com/extras/package/cacheguard2
cacheguard registers an ExtJS script to uncheck the "Empty Cache" checkbox when a Manager user edits an unpublished Resource. On the save event, it clears the cache for that one Resource only so new changes can be viewed.
Use case example: if your site gets frequent content updates, then most likely your content editors frequently save their work-in-progress. By preventing the cache from being cleared on these frequent save events, the cache for Live Resources will survive longer and help reduce server load. Can be manually overridden on every save event.
Alternate Cache Providers
Out-of-the-box, MODX also supports these alternative cache providers:
- APC Cache: http://rtfm.modx.com/display/xPDO20/Caching
- memcached: http://modx.com/blog/2012/09/24/using-memcached-for-modx-caching/
Of course you need to ensure the required PHP extensions are installed on your server. In multi-node environments you can use the Remotes plugin to synchronize the disparate local caches. These two providers cache in memory rather than on the filesystem, so as long as you've provisioned sufficient RAM, you will gain noticeable speed improvements.
...MODX is the Hand of the King!
You can do pretty much anything in MODX. It affords you Total Creative Freedom. With these Add-on Packages, you also have Total Cache Control.