We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 46718
    • 39 Posts
    I have an import snippet that updates some resources and system settings, then saves them. However, after doing some testing I confirmed that saving the system setting is forcing a full site cache refresh.

    Here is a simple example of the code I am using:
    $setting = $modx->getObject('modSystemSetting', 'my_setting');
    $setting->set('value', 'Some Value');
    $setting->save();
    


    This is a part of a larger snippet, but whenever I run the snippet with this code in my full site cache gets refreshed, whenever I remove it, it doesn't. Based on what I've read, I thought that you had to specifically clear the site cache or the settings cache, etc. However, this is not what seems to be happening.

    Am I missing something, or is this the expected behavior?

    Ideally, I'd like to just clear the system setting cache when I save the system settings. Once that works, I'd like to keep track of the resources I update and clear the cache on only those resources, not the whole site.

    Thanks,
    Roger

    This question has been answered by smashingred. See the first response.

      • 3749
      • 24,544 Posts
      Are you sure that's happening? I haven't tested that in particular, but I've never run into a case where modifying the DB in code affected the cache unless you go through a processor. Usually, I have the opposite problem since the changes don't show up until the cache is cleared.
        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
        • 46718
        • 39 Posts
        Yeah, I removed everything else in the snippet except that code and when I run it the whole site cache clears and when I comment it out it doesn't.

        I know it seems weird to me.

        Any other ideas of ways to test?
          • 3749
          • 24,544 Posts
          There's an error in your code I hadn't noticed. It should be:

          $setting = $modx->getObject('modSystemSetting', array('key' => 'my_setting'));


          I tested your code with the change and it has no effect on the cache.
            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
            • 46718
            • 39 Posts
            I've narrowed this down to an issue with 2.2.10. Here's what I did to verify.

            Testing Setup
            1 - Created a system setting called "api_import_date"
            2 - Created two snippets: "rightNow" & "cacheTest"
            3 - Created two pages, one to call each snippet
            4 - Call "rightNow" cached
            5 - Call "cacheTest" uncached

            Snippet Contents:

            rightNow:
            $rightNow = date('n/j/Y g:i:s A');
            return 'current time: '.$rightNow;


            cacheTest:
            $rightNow = date('n/j/Y g:i:s A');
            $setting = $modx->getObject('modSystemSetting', array('key'=>'api_import_date'));
            $setting->set('value', $rightNow);
            $setting->save();
            return 'Cache test run<br>'.$rightNow;



            Expected Behavior - Occurs on 2.2.14
            1 - Load rightNow resource to cache the current time
            2 - Load cacheTest resource to save system setting
            3 - Reload rightNow resource and see that time has not changed and is reading from cache


            Unexpected Behavior - Occurs on 2.2.10
            1 - Load rightNow resource to cache the current time
            2 - Load cacheTest resource to save system setting
            3 - Reload rightNow resource and see that time has changed and site cache has been cleared
            • discuss.answer
              • 27708 MODX Staff
              • 2,502 Posts
              Roger,

              It looks like in 2.2.11 there was a change:
              Remove cache clearing logic from system setting model
              Not sure if this had any impact on what you're doing, but it may have.
                Author of zero books. Formerly of many strange things. Pairs well with meats. Conversations are magical experiences. He's dangerous around code but a markup magician. Blog ✦ Twitter ✦ LinkedIn ✦ GitHub