We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 41874
    • 33 Posts
    Hi all,

    What are the best practices for caching a snippet with some custom data calls?

    I'm interested in the caching the output of a custom snippet after some db-calls and data processing stuff. First I've a question about caching details by not using the not-cache flag [[!any]], second I'm interested in the comparison of the cache flag and the use of cacheManager in a snippet.

    1.
    Does caching using the cache flag at a snippet takes into account arguments? e.g. [[snippet? &arg=`1`]] and [[snippet? &arg=`2`]]. Are these snippet calls cached separately?

    2.
    Assuming the cache flag takes into account the arguments (as asked in 1.);

    Which caching method is better?

    1. Without the cache flag at the snippet call; [[snippet? &arg=`1`]]. If so, how can I clear this snippet cache when custom objects change?
    2. With the cache flag, using the cacheManager; [[!snippet? &arg=`1`]] using $modx->cacheManager within the snippet.

    I appose option 1 is quicker, because it saves a snippet call including some cache calls, and the cache is saved together with the resource (??).

    Thanks in advance!

    Huub
      I love flexibility
      • 39465
      • 14 Posts
      http://rtfm.modx.com/revolution/2.x/developing-in-modx/advanced-development/caching

      This is a good read on Caching I think.

      Usually I just use getCache snippet (available on addons), and wrap a custom snippet, cache it to custom partition. Then you can create an action menu to just clear that partition, or do that elsewhere..

      http://modx.com/blog/2012/10/29/optimization-with-getcache-and-custom-cache-partitions/

      This is also a good read.

      Alex
      • See http://rtfm.modx.com/xpdo/2.x/advanced-features/caching/caching-tutorial-lifetimes

        If you don't use a custom partition, the cached stuff will get cleared when the site's cache is cleared. Otherwise, you should write a CMP to clear your custom partition or create a plugin to clear the custom partition (e.g. tie it to the regular cache-clearing event), e.g. something simple like
        $cache_dir = 'your_custom_partition';
        if ($modx->event->name == 'OnBeforeCacheUpdate') {
            $dir = MODX_CORE_PATH .'cache/'.$cache_dir;
            $objects = scandir($dir);
            foreach ($objects as $object) {
                if ($object != '.' && $object != '..') {
                    if (filetype($dir.'/'.$object) != 'dir') {
                        @unlink($dir.'/'.$object);
                    } 
                }
            }
            reset($objects);
        }
          • 41874
          • 33 Posts
          Thanks both for the replies!

          I am familiar with the custom caching partitions, the reason I raised this question is because I'm interested in the normal behavior of the snippet caching by MODx. When using a snippet call with dynamic parameters it looks like only the result of the first request to the snippet is cached, and the snippet code itself.

          An Example:

          1. A document content contains: [[foo? &arg=`[[!getUrlParam? &name=`bar`]]`]]
          2. Then this page is requested with parameter bar filled (index.php?id=42&bar=1)
          3. Now this page is requested with another value in that parameter (index.php?id=42&bar=2)
          4. Then looking into the resource cache (/core/cache/resource/web/resources/42/*.cache.php)
          5. ! - I found the snippet code itself (of foo), the snippet call with the first parameter value [[foo? &arg=`1`]], and the snippet call with the getUrlParam call in it [[foo? &arg=`[[!getUrlParam? &name=`bar`]]`]].

          What I'm missing is the caching of all the other snippet outputs, dependent on their input values. Is this default behavior, and why does the system not uses a cache for snippet outputs?

          ===================
          Btw: I'm now using a custom cache partition for the snippet result, but was (and still am) wondering if a normal snippet cache (described above the horizontal line) has a better performance.
            I love flexibility