We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 38593
    • 129 Posts
    Is it possible to automatically flush the cache and reload all pages?

    Our website uses two servers, one that has the MODX website and another that contains product inventory and pricing. Because these two servers are in different cities, there is a noticeable time lag as the snippets query the remote database while building pages.

    I know one option would be to fetch the current product data and store it locally in MySQL and then rewrite the snippets to use the local copy.

    But was wondering if flushing the cache and then rewriting all the pages back into the cache (only 24 product pages) could be done through MODX, and then perhaps set up to be run via CRON every few hours. This way up-to-date pages could be served from the cache to all visitors. And since the page building is done behind the scenes, the database lag would not be an issue.
      Pedalers Bicycle Tours my website forged with MODX
      vandergraaf-M static website generator for MODX
      • 38878
      • 255 Posts
      Haven't done it but you can take a look at modCacheManager http://rtfm.modx.com/display/revolution20/Caching#Caching-UsingmodCacheManager%3A%3Arefresh%28%29%28forMODX2.1andlater%29.

      Writing something like the examples provided you could selectively refresh cache by calling the snippet through cronmanager.
        • 40045
        • 534 Posts
        Have a look at Bobs RefreshCache Extra http://modx.com/extras/package/refreshcache!
          • 38878
          • 255 Posts
          That will do it and do away with the need for cronmanager.

          I think for MrCycling's need it would be optimal to refresh the 24 product resources only and periodically to reduce load times on the rest. Like a snippet on a resource (or in the cron) like Bob's that monitors specific resources (configurable), the time since the pages were last cached, and refreshes when the threshold is reached. Maybe there is something like this already. I haven't really hunted around for it.


          Quote from: exside at Jan 24, 2013, 06:09 AM
          Have a look at Bobs RefreshCache Extra http://modx.com/extras/package/refreshcache!
            • 38593
            • 129 Posts
            Thanks for the suggestions. Bob's snippet seems to be manually run through a browser, so won't really work for us as it needs to be automatic a couple of times a day. Plus it mentions that it only refreshes page that are indexed and our product pages are not using the MODX indexing system. But yes as Harvey EV mentioned, we really only need to refresh the product pages.

            Will have to explore the modCacheManager some more to see if I can sort out a CRON job using it.
              Pedalers Bicycle Tours my website forged with MODX
              vandergraaf-M static website generator for MODX
              • 3749
              • 24,544 Posts
              Are you updating the pages with a snippet? If so, this will delete the cache for just one resource:

              $key = $ctxResource->get('context_key');
                  $cKey = str_replace($mgrCtx, $key, $ck);
                  $modx->cacheManager->delete($cKey, array(
                        xPDO::OPT_CACHE_KEY => $modx->getOption('cache_resource_key', null, 'resource'),
                        xPDO::OPT_CACHE_HANDLER => $modx->getOption('cache_resource_handler', null,
                        $modx->getOption(xPDO::OPT_CACHE_HANDLER)),
                        xPDO::OPT_CACHE_FORMAT => (integer)$modx->getOption('cache_resource_format', null,
                        $modx->getOption(xPDO::OPT_CACHE_FORMAT, null, xPDOCacheManager::CACHE_PHP))
                 )
              );


              You could then refresh the cache for that page by accessing it with cURL.

              It might make more sense, though, to use modCacheManager for this.

              I think this would refresh the cache for all resources in the web context:

              /* refresh the web context resources */
              $modx->cacheManager->refresh(array(
                  'resource' => array('contexts' => array('web')),
              ));
              



                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
                • 38878
                • 255 Posts
                It looks to me like if you explicitly define the resources you want to refresh, then modCaheManager should do the trick for you. Even easier would be to manage the list of resources to refresh if you pass them to the snippet using the snippet properties field right in the cronmanager instead of hard coding in the snippet.

                Cheers
                  • 3749
                  • 24,544 Posts
                  Harvey, do you have the code necessary to refresh the cache for a single resource using modCacheManager? I looked, but couldn't find an example other than the one the refreshes all resources in a context.
                    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
                    • 38878
                    • 255 Posts
                    @Bobray. Well since I'm on my iPad its a bit tough to code ATM. But just looking at your second code reference below passing an argument representing a list of the resources to refresh to the array should be easy peasy since MCM obviously can take an array of resources as input. I'll try to whip one up and test it and share it when I can though unless someone beats me to it.
                      • 38593
                      • 129 Posts
                      BobRay - Yes the data is called through a snippet when the page is built. I had considered simply deleting the cached copy whenever a sale was made, but now product is sold through multiple websites, so I need to go automated to refresh data on a regular basis.

                      The modCacheManager looks like the way to go, but as you have mentioned there aren't too many examples to work off of ... and I am a total noob when it comes to coding.
                        Pedalers Bicycle Tours my website forged with MODX
                        vandergraaf-M static website generator for MODX