Cache is as Good as Gold

Data processing is money, and waiting for responses can compound your site's request queue in high load situations. Whether you have a custom database or are working with external libraries, caching those responses is one of the easiest ways to speed up and scale your site.

Utilizing $modx->cacheManger

MODX has a built-in caching mechanism called cacheManger. This system has various functions, which can be invoked via a $modx->cacheManager call.

One of the cool things about the cache manager is you have the ability to save cached responses anywhere. You can utilize the standard cache, or you can separate it so that the "Clear Cache" button doesn't affect your cached responses. In the latter scenario it is important to set up alternate methods for clearing the cache, so you don't wind up with a stuck response. 

Basic Usage

There are two primary things to understand when working with the MODX Cache Manager, setting a cache and retrieving a cache. Once you understand those, you can inject the two events based on various interactions with your script. If your data changes based on external changes, you can set the cache to expire at set times throughout the day. However, if a specific manager interaction occurs you can set processes within that interaction to recache the values.

Setting Cache

To set a cache response you need to pass four elements to the cache manager:

  1. A key. This is the unique identifier for the specific cached response. 
  2. Data. This is the values you want to be associated with the key. 
  3. The lifetime. This is the number of seconds you want the cache to be stored. If you only want the cache to be removed manually, you will set this to 0.
  4. Options. Options are where you determine the cache type and path. 

For example you could have: 

$result = $WhateverYouWanttoCache;
$options = array(xPDO::OPT_CACHE_KEY => 'path'); // cacheManager folder path
$key = $modx->resource->id . "-cachereponse"; // unique key for the cache
$time = 1800; // 30 minutes (60 * 30)

$modx->cacheManager->set($key, $results, $time, $options);

Retrieving the cache

To retrieve the cache you just need to check if the key and options return a value:

$options = array(xPDO::OPT_CACHE_KEY => 'path'); // cacheManager folder path
$key = $modx->resource->id . "-cachereponse"; // unique key for the cache

$results = $modx->cacheManager->get($key, $options);

if(empty($results){$results = processSomethingUncached();}

At the end of the function "processSomethingUncached" you would ideally set the results in the cache. 

Replacing or Deleting Results

Depending on your needs you can replace or delete your cache.  This is best used in a custom manager page or plugin when you know when and how the values will be updated:

$result = $WhateverYouWanttoCache;
$options = array(xPDO::OPT_CACHE_KEY => 'path'); // cacheManager folder path
$key = $modx->resource->id . "-cachereponse"; // unique key for the cache
$time = 1800; // 30 minutes (60 * 30)

//Replace
$modx->cacheManager->replace($key, $results, $time, $options);

//Delete
$modx->cacheManager->delete($key, $options)

Wrap up

The MODX cacheManager is a powerful tool that will help you reduce requests and parsing on your site. By leveraging it on your responses, you can prevent a lot of unnecessary processing time. Remember that when you call a snippet uncached, the system just caches the snippet itself, not the response. If you want to lower requests for something like a Google Calendar XML feed, you would just apply the built-in cache manager to collect and then render those responses.