HTTP/1.1 // 38.107.191.89 // // 54041 // // CCBot/1.0 (+http://www.commoncrawl.org/bot.html)

ThinkGeek - Cool Stuff for Geeks and Technophiles

How To Use a MODx Plugin to Cache Snippets

Jan
28
2008

I don’t want to take credit for this, but I definitely wanted to blog about it because I think it is a big deal. In a forum post I made on modxcms.com, ganeshXL came up with an awesome way to cache snippet output with a MODx plugin. Here is what ganeshXL wrote:

  1. create a chunk. Leave it empty. Name it whatever you want, e.g. testMenu

  2. create a plugin. Enter this:

    $myChunk = 'testMenu'; // your chunk name
    $params['startId'] = '0';
    $params['level'] = '4';
    $html = addslashes($modx->runSnippet('Wayfinder', $params));
    $sql= "UPDATE " . $modx->getFullTableName("site_htmlsnippets") . " SET snippet='$html' WHERE name='$myChunk'";
    $result= $modx->dbQuery($sql);

  3. Check the system event OnDocFormSave + save the plugin.
  4. Change your template: Replace your Wayfinder call with your chunk: {{testMenu}}

Now, every time you edit a doc, the Menu code is being regenerated and stored in your chunk. This should speed up things quite a bit..

This is really cool since it means that you can cache snippet output and give php and mysql a break on processing your code. Like I said, all the credit goes to ganeshXL :) If you’re interested in the context that this was created in, you can read the full post here.


3 Comments »

Allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
MyAvatars 0.2

Comment by Dragan (ganeshXL)
on 18 Apr 2008 at 10:51 am # Subscribed to comments via email

Howdy! Surprised to find my little forum post here (I googled for something modx-related stuff); glad you found it useful.

MyAvatars 0.2

Comment by James Ehly
on 21 Apr 2008 at 9:44 pm #

Thanks, I really did find your solution useful. I ended up modifying the final code to run from a module rather than a plugin, which worked out well for my client. The particular code just took too long to process after every doc save.

 
 
MyAvatars 0.2

Comment by e-Stonia
on 22 Aug 2009 at 8:42 am #

Thanks a lot! Really useful!