We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 6531
    • 154 Posts
    I want to include a button on my site to clear the cache.
    How would I create such a snippet?
    thanks
      • 28215
      • 4,149 Posts
      For Revo:
      $modx->executeProcessor(array(
         'location' => 'system',
         'action' => 'clearCache',
      ));
      return '';
      
        shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
        • 6531
        • 154 Posts
        awesome! thanks!
          • 6531
          • 154 Posts
          Hmm ... actually, it doesn’t seem to be working. It ’runs’ almost instantly, and it does not actually clear the cache.
          Any idea why?
            • 22303 MODX Staff
            • 10,725 Posts
            You can only execute that as a user who has empty_cache permission for the web (or other context) from which the command is called. If you wanted this to be run by any anonymous user, you would need to create a policy with the empty_cache permission and assign it to the anonymous user group for the Context( s ) you want it to be executable on.
              • 6531
              • 154 Posts
              Aha. I see. So can I just ’add’ the ’empty_cache’ permission to the ’Load Only’ policy?

              While awaiting your response, I ended up lifting the following from [tt]/manager/controllers/default/system/refresh_site.php[/tt]
              It seems to work -- but I guess your method is preferable?

              <?php
              $modx->invokeEvent('OnBeforeCacheUpdate');
              $paths = array(
                  'config.cache.php',
                  'sitePublishing.idx.php',
                  'registry/mgr/workspace/',
                  'lexicon/',
              );
              $contexts = $modx->getCollection('modContext');
              foreach ($contexts as $context) {
                  $paths[] = $context->get('key') . '/';
              }
              $options = array(
                  'publishing' => 1,
                  'extensions' => array('.cache.php', '.msg.php')
              );
              if ($modx->getOption('cache_db')) $options['objects'] = '*';
              
              $results= $modx->cacheManager->clearCache($paths, $options);
              
              return "Done!";

                • 22303 MODX Staff
                • 10,725 Posts
                It is preferable in most cases to use the processors, as they centralize the standard processes that make up the Content Management features of the core. Yes, you can just add the empty_cache to Load Only but I’d just copy the Policy and call it something else for posterity; then you can change the ACL you already have Load Only assigned to.

                You method is correct, but remember, if anything changes in the core, you’ll need to maintain your code accordingly, whereas if you used the processors, that would not be necessary.