We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 8784
    • 32 Posts
    I'm trying to remove the cache file for just one resource, not having any luck.

    First attempt:
    $resFive = $modx->getObject('modResource',5);
    $cacheKey = $resFive->getCacheKey();
    $modx->cacheManager->delete($cacheKey);
    


    Second attempt:
    $resFive = $modx->getObject('modResource',5);
     $modx->cacheManager->refresh(array(
    	'db' => array(),
    	'auto_publish' => array('contexts' => array($resFive->get('context_key'))),
    	'context_settings' => array('contexts' => array($resFive->get('context_key'))),
    	'resource' => array('contexts' => array($resFive->get('context_key'))),
    ));
    


    Is there any in danger just using php?:
    $resFive = $modx->getObject('modResource',5);
    $cacheKey = $resFive->getCacheKey();
    $file = $path_to_cache.$cacheKey.'.cache.php'; //Something like: 'cache/resource/web/resources/5.cache.php
    unlink($file);
    

    [ed. note: elvispresley2k last edited this post 12 years, 5 months ago.]
      • 3749
      • 24,544 Posts
      Try something like this:

      $modx->getCacheManager();  // harmless and sometimes necessary
      $modx->cacheManager->delete('resource/web/resources/5.cache.php');

        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 8784
        • 32 Posts
        BobRay,

        Thanks for the suggestion, unfortunately this does not work either.

        For what it's worth, here's my full code. This is a snippet:

        <?php
        // This bit works fine:
        $cm = $modx->getCacheManager();
        $resource = $modx->getObject('modResource',1);
        $results = $cm->generateResourceHack($resource);
        
        // This bit does not work:
        $cm->delete('resource/web/resources/5.cache.php');
        
        • In MODX 2.1, you need to get the provider for the "resource" cache partition first:

          <?php
          /* get the cache provider for the "resource" cache partition */
          $resourceCache = $modx->cacheManager->getCacheProvider($modx->getOption('cache_resource_key', null, 'resource'));
          
          $resFive = $modx->getObject('modResource', 5);
          $resourceCache->delete($resFive->getCacheKey());
          
          [ed. note: opengeek last edited this post 12 years, 5 months ago.]
            • 8784
            • 32 Posts
            Hey opengeek,

            Unfortunately "getProvider" method isn't found, throws the error:
            Fatal error: Call to undefined method modCacheManager::getProvider() in mySnippet

            I can only find 1 method called, "getProviders" in the "modTransportManager" class file. It's not in either "xpdocachemanager.class.php" or "modcachemanager.class.php".

            I'm using 2.1.3-pl (traditional), if that would make a difference.

            • There seems to be a getCacheProvider method in 2.2.
                Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

                Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.
              • My apologies for posting the wrong method name. I did mean to say getCacheProvider(). Code updated and you can learn more about how MODX utilizes specific cache partitions at http://rtfm.modx.com/display/xPDO20/Caching#Caching-UtilizingSpecificCachePartitions.
                  • 8784
                  • 32 Posts
                  No worries OpenGeek! I appreciate your help! "getCacheProvider()" works like a charm.

                  But now another (hopefully small) issue:
                  I want to fire a plug-in whenever a resource is saved in the manager, after the cache is cleared.

                  I have a plug-in, "onSiteRefresh" and "onCacheUpdate" are both checked:
                  <?php
                  $modx->setLogLevel(modX::LOG_LEVEL_DEBUG);
                  $modx->log(modX::LOG_LEVEL_DEBUG, '############test create cache file');
                   
                  $to = "[email protected]";
                  $subject = "Test mail";
                  $message = "Hello! This is a simple email message.";
                  $from = "[email protected]";
                  $headers = "From:" . $from;
                  mail($to,$subject,$message,$headers);
                  


                  It never fires. When a resource is saved, no log is written and no email is sent.

                  I must be missing something obvious. sad
                  • Try OnDocFormSave instead of OnSiteRefresh - I suspect the latter to only fire when someone clears the cache manually.
                      Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

                      Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.
                      • 8784
                      • 32 Posts
                      Hi Mark,

                      Yes the plug-in fires perfectly when I use "OnDocFormSave" when a resource is saved in the manager.

                      The problem is the "OnDocFormSave" event is invoked BEFORE the cache is refreshed in the file:
                      'core/model/modx/processors/resource/update.php'.
                      In this file, no other events are invoked after "OnDocFormSave". sad


                      I'm trying to generate a cache file manually. If my manually created cache file is created before 'modx->cacheManager->refresh' happens, it will just get deleted.