We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 34178
    • 212 Posts
    Can anybody tell me where I have to add code-lines to send an email to me when somebody clears the cache (either directly or by saving/updating a resource/chunk or snippet)? In 'System Events' of Plugins I found OnBeforeCacheUpdate, OnBeforeSaveWebPageCache, OnCacheUpdate and OnLoadWebPageCache. Is one of these events maybe proper to realize this?

    And can I add informations in the email who has cleared the cache by which action?

    Does anybody know this? Would be great!

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

      • 28042 ☆ A M B ☆
      • 24,524 Posts
      OnSiteRefresh is, I believe, the event for when the Manager menu item is used. Any plugin using that event could easily enough indicate who did it, in fact that information is available in the manager actions report.

      As far as when the cache is cleared automatically, I don't know of a way to detect that. Using On<Whatever>Save wouldn't be very reliable, because the "clear cache on save" checkbox could always have been cleared so the cache wouldn't have been cleared.

      There isn't any way for MODX to detect when someone manually deletes the cache. That would have to be done by some kind of server script, such as iWatch http://iwatch.sourceforge.net/documentation.html
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 34178
        • 212 Posts
        Hi Susan,
        maybe I can add some code-lines to the snippet which empties the cache (to send me an email on emptying the cache)? Do you know which snippet/file empties the cache?
        • discuss.answer
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          Everything uses the cacheManager function, with variations on the parameters sent
          $this->modx->cacheManager->refresh()

          That refresh() function is found in core/model/modx/modcachemanager.class.php on line 423.

          Just out of curiosity, why do you want an email notification of all kinds of cache clearing?
            Studying MODX in the desert - http://sottwell.com
            Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
            Join the Slack Community - http://modx.org
            • 28042 ☆ A M B ☆
            • 24,524 Posts
            There are also system events for a user deleting folders and files using the Manager's file browser - OnFileManagerDirRemove and OnFileManagerFileRemove.
              Studying MODX in the desert - http://sottwell.com
              Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
              Join the Slack Community - http://modx.org
              • 3749
              • 24,544 Posts
              It would be easy to do, but you'll get an email every time a resource, TV, Template, Chunk, Snippet, or Plugin is saved in the Manager and your code could be overwritten when you upgrade MODX.

              There is a System Event called OnBeforeCacheUpdate. I think it only fires when the user clears the cache manually, but I'm not sure. You could connect a plugin to it that sent and email (or for a quick check, just writes to a chunk or file) and find out.

              Create a chunk called 'CacheUpdates'.

              Put this code in a plugin attached to OnBeforeCacheUpdate:


              $chunk = $modx->getChunk('CacheUpdates');
              
              if ($chunk) {
                 $content = $chunk->getContent();
                 $chunk->setContent($content . "\n" . 'OnBeforeCacheUpdate fired: ' . date("D M d, Y G:i a"));
                 $chunk->save();
              }
              


              You can look a the chunk and see how often the cache was cleared. If it does what you want, send an email in the plugin code instead of writing to the chunk.
                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
                • 28042 ☆ A M B ☆
                • 24,524 Posts
                Examining the modcachemanager.class.php file, it has this at the very end
                        /* invoke OnCacheUpdate event */
                        $this->modx->invokeEvent('OnCacheUpdate', array(
                            'results' => $results,
                            'paths' => $paths,
                            'options' => $options,
                        ));
                

                So it looks like a plugin using onCacheUpdate might do something.

                However, that would only be for any time MODX is used to clear the cache. Actually deleting files using the File Manager or FTP would require other means. Using the File Manager can be dealt with in the plugin by listening for those two events, but deleting files via FTP or SSH is done without MODX ever being run, so MODX would have no way of monitoring such activity. It is possible for MODX to scan the file system and report changes when it is executed, but that's all it could do. Evolution has a security feature that checks certain key files to report changes to them, displaying the report in a tab in the Welcome page.
                  Studying MODX in the desert - http://sottwell.com
                  Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                  Join the Slack Community - http://modx.org
                  • 28042 ☆ A M B ☆
                  • 24,524 Posts
                  No, that event is invoked at the end of the clearCache() function, so it looks like that's only invoked in the case of using the Clear Cache menu item.

                  So BobRay's solution would have to be used for elements, but you'd need to check to make sure the "Clear Cache on Save" checkbox was checked or you'd possibly get false positives.
                    Studying MODX in the desert - http://sottwell.com
                    Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                    Join the Slack Community - http://modx.org
                    • 34178
                    • 212 Posts
                    Hi Susan and Bob,
                    thank you so much!