We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 14263
    • 4 Posts
    Hi there,
    first of all i would like to say, that i love MODx for its simplicity! The idea to split the templates into chunks, snippets, modules etc. is brilliant.

    I already successfully wrote some basic snippets, used the DBAPI and chunks from within the snippets.

    Now i have got some snippets that fetch information from the db which is valid for several hours. I would like to control the caching by myself, can i use MODx cache functions within my code?

    Is there something like $modx->getCache($key), $modx->putCache($key, $value) or something similar?

    best regards,
    Juri

    PS: where is the file containing the class for $modx?
      • 22303 MODX Staff
      • 10,725 Posts
      There are some functions like this in the upcoming 0.9.7 release (which is available as an alpha now), but nothing like this currently exists in 0.9.6.x or previous versions.
        • 14263
        • 4 Posts
        Hi, thank you for the hint. I guess i dig into the new release.

        Thanks

          • 22303 MODX Staff
          • 10,725 Posts
          Quote from: jk_nice_coding at May 06, 2008, 01:06 PM

          Hi, thank you for the hint. I guess i dig into the new release.
          There are some much more in-depth caching mechanisms available in the new branch (which has been rewritten from the ground up over the last two years), but it’s not quite ready for prime-time just yet.

          In the current/prior releases, the $modx object is driven by a class called DocumentParser, which is found in manager/includes/document.parser.class.inc.php. In the new code it’s the modX class in core/model/modx/modx.class.php.
            • 14263
            • 4 Posts
            I use a super-simple solution in 0.9.6, maybe someone can use this:

            Here’s a sample snippet, which handles caching by itself. Cache is refreshed every 5000 requests. Instead of refreshing every x requests, you can use the touch function on $cachepath and set a time in future and check it with filemtime for validity.
            <?php
              $refresh_every_requests = 5000; // refresh about every 5000 requests
              $cachepath = MODX_BASE_PATH . 'assets/cache/owncacheprefix__' . md5($this->currentSnippet);
            
              $content = file_exists($cachepath)?file_get_contents($cachepath):false; // get cached contents
              if (!$content || rand(0, $refresh_every_requests) == 1) {
                // if content not cached, or its time for a refresh then generate content
                // run db queries / api requests and other time consuming tasks
                $content = "SNIPPETCONTENT"; // set content to cache
                file_put_contents($cachepath, $content); // save to cachefile
              }
              return $content;
            
            ?>
            

              • 28042 ☆ A M B ☆
              • 24,524 Posts
              You should use return for a snippet’s output, not echo.
                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
                • 14263
                • 4 Posts
                Thanks for the hint.

                Quickedit messed up the post before.

                <?php
                  $refresh_every_requests = 5000; // refresh about every 5000 requests
                  $cachepath = MODX_BASE_PATH . 'assets/cache/owncacheprefix__' . md5($this->currentSnippet);
                
                  $content = file_exists($cachepath)?file_get_contents($cachepath):false; // get cached contents
                  if (!$content || rand(0, $refresh_every_requests) == 1) {
                    // if content not cached, or its time for a refresh then generate content
                    // run db queries / api requests and other time consuming tasks
                    $content = "SNIPPETCONTENT"; // set content to cache
                    file_put_contents($cachepath, $content); // save to cachefile
                  }
                  return $content;
                
                ?>