We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 9207 ☆ A M B ☆
    • 2,475 Posts
    I think I had this conversation before, but I'm wondering if it's possible to cache custom data. Specifically, I'm looking to build some image manipulation output filters (e.g. to automatically make and cache resized images).

    I can figure out all the PHP image resizing pretty easily, and it looks like I can write the path to the new images using the MODX cache manager:
    http://rtfm.modx.com/display/revolution20/Caching

    But will that work? E.g. imagine an output filter like this:

    [[*path_to_my_image:thumbnail=`150x200`]]


    So the output filter gets an image path as input, e.g. http://path/to/my.jpg... is there a "correct" way to write the new file to the MODX cache directory? Can I just use the modFileHandler service to write the file to the cache directory (http://rtfm.modx.com/display/revolution20/modFileHandler)? Once the file has been moved into some directory inside of the cache directory, I can use the cache manager to store the path to the image and refresh it as needed, but do I also need to handle the OnBeforeCacheUpdate event to clear out the custom image directory I created inside of the cahce directory?
      • 33968
      • 863 Posts
      I do a lot of custom data caching - I found the best way to understand it is to download the getCache snippet and see how it is done there. You can easily adapt it to how you want your own data to be cached.
        • 9207 ☆ A M B ☆
        • 2,475 Posts
        Thanks for the tip, Lucas! I'll check it out.
          • 33968
          • 863 Posts
          Also have a look here, there were some good tips from opengeek and splittingred:
          https://forums.modx.com/thread/69839/using-modx--cachemanager--refresh-with-custom-components
            • 9207 ☆ A M B ☆
            • 2,475 Posts
            I realized belatedly that I can't write images to the core/cache directory because images need to be INSIDE the document root and the cache directory can be OUTSIDE the document root. Caching images really is a special use case that doesn't seem to need the MODX cache manager.

            So how can I hook into an "empty cache" event? Is there such an event? I've tried writing a plugin that will clear the custom directory, but using the OnBeforeCacheUpdate, OnCacheUpdate or OnSiteRefresh events didn't seem to be executed during the cache-clearing.
              • 33968
              • 863 Posts
              I use the following plugin tied to OnBeforeCacheUpdate and it clears out my custom cache data whenever I clear the regular site cache.

              <?php
              if ($modx->getCacheManager()) {
                  $modx->cacheManager->delete('', array(xPDO::OPT_CACHE_KEY => 'custom_cache_key'));
              }
              


              I also created a separate action to only clear my custom data as it changes much more often than my resources do.
                • 9207 ☆ A M B ☆
                • 2,475 Posts
                Sorry, I'm still confused... I understand that the CacheManager let me cache data, e.g. a URL to an image, but it's important that I can store the image represented by that URL to a public directory. I don't see if I way I can write "cache" data to an arbitrary folder.

                My cache-clearing plugin is currently just a message to the MODX error log -- but I can't seem to make it fire, no matter which even I've attached it to.