We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 35781
    • 25 Posts
    I have a snippet that creates tree structure for resource. I want to cache this tree for every resource that call snippet (snippet call is somewhere in the resource template), but this method only works for one snippet call. If I open another resource it will output cache of first snippet call. Can I solve this issue using modx native methods?
      • 22303 MODX Staff
      • 10,725 Posts
      All Snippets are cached per Resource automatically into the Resource cache. You do not need anything special to achieve that.

      Do you want this structure cached beyond the scope of the individual Resource caches?
        • 35781
        • 25 Posts
        Snippets are cached per Resource, but what about cacheManager->set(var,value) ? Does this vars cached per resource?

        There is snippet example:
        $contents_cache = $modx->cacheManager->get('contents_cache');
        
        if(!empty($contents_cache)){
                $contents = $contents_cache;
            }
        else{
        // do some work
        $modx->cacheManager->set('contents_cache',$contents_cache);
        }
        

        This snippet is called in the resource template.

        When I open one page, my vars are filled right. But when I open another page it contents old (cached) var value.
          • 35781
          • 25 Posts
            • 22303 MODX Staff
            • 10,725 Posts
            You can specify what xPDOCache instance your cache items are stored in. If you take a look at the getCache Extra, you can see that it, by default, stores it's cache items in the resource cache partition, but you can specify a cacheKey to target another existing, or even custom partition to store your items.

            Consider if you wanted to cache the output of a Snippet longer than the Resource cache; you can target a custom cache partition that is not cleared by the normal cache refresh actions in MODX, and create a plugin attached specifically to events you want to trigger the partition to be flushed. I'm working on a blog post covering this topic to bring more attention to the flexible caching capabilities that are already at the disposal of MODX site developers.