We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 19975
    • 429 Posts
    Is is possible to cache part of a snippet? I am developing a snippet to fetch data from an RSS feed and then return results including a special "success" css class if the RSS attribute is included in the url parameter.

    As fetching the RSS feed on every snippet run is unnecessary and inefficient is it possible to cache the xPath function of my snippet?


    <?php
    $i = 0;
    $title_a = $value_a = [];
    
    $XML = new DOMDocument();
    $XML->load($url);
    $xpath = new DOMXPath($XML);
    
    foreach ($xpath->query("//channel/item/season/*/title") as $title_node) {
    	$title_a[] .= $title_node->nodeValue;
    }
    foreach ($xpath->query("//channel/item/season/*/value") as $value_node) {
    	$value_a[] .= $value_node->nodeValue;
    }
    
    $season_a = explode(',',$_GET['season']);
    
    while ($i <= 12) {
    	$output .= '<div class="col-xs-6">';
    	if(in_array($i, $season_a)){
    		$output .= '<a href="" class="btn-xs btn-block filter-pill btn-success">';
    		$output .= $title_a[$i];
    		$output .= '<i class="icon-cross"></i></a>';
    	} else {
    		$output .= '<a href="" class="btn-xs btn-block filter-pill btn-default">';
    		$output .= $title_a[$i];
    		$output .= '<span class="badge pull-right">' . $value_a[$i] . '</span></a>';
    	}
    	$output .= '</div>';
        $i++; 
    }
    return($output);
      Martin Sanders - Design & Web Development
      • 3749
      • 24,544 Posts
      I think an easier approach might be to create a snippet that fetches the RSS feed XML and writes it to a file. Have a cron job that runs that snippet at intervals and have your program use the file's contents instead of fetching the feed. It will not only be more efficient, but the snippet will run many times faster.
        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
        • 19975
        • 429 Posts
        Great idea Bob, I have just tested loading a static RSS file and trimmed a full 1s off the server response time, this is a significant change from 1.3s to less the 400ms.

        The only issue tho is that pThumb no-longer seems to like my images.
          Martin Sanders - Design & Web Development
          • 3749
          • 24,544 Posts
          If the image tags are in an included file, the paths will be relative to that file's location. Could that be it?
            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