I have several (24) external xmlfeeds showing products on my frontpage. See this stackoverflow question for the details of the feeds. Each feed shows one product, but it takes ages to load those feeds each time someone visits my site. The feeds are updated once a day.
What is the best way of caching these feeds? Should i do something with mysql or save xml files locally. I’m not sure where to start. I tried JG_Cache, but without luck.
This is the loop i use:
<?php
// get xml file contents
$xmlurl = get_post_meta( get_the_ID( ), 'plus_xmlfeed', true );
if ( !empty( $xmlurl ) ){
$xml = simplexml_load_file($xmlurl);
}
$xml->product->name = substr($xml->product->name, 0, 30).'...';
$desc = substr($xml->product->name, 0, 50).'';
?>
<li class="span3">
<div class="thumbnail">
<a href="<?php echo $xml->product->productURL ;?>">
<center><img src="<?php echo get_template_directory_uri(); ?>/img/logos/<?php echo get_post_meta(get_the_ID( ), 'plus_logos', true); ?>.gif"/></center>
<center><img class="mainimg" alt="" src="<?php echo $xml->product->imageURL ;?>"/></center>
</a>
<div class="caption">
<h5><a href="<?php echo $xml->product->productURL ;?>"><?php echo $xml->product->name; ?></a></h5>
<div class="maindesc half clearfix"><div class="prijs">€ <?php echo $xml->product->price ;?></div><strike style="color: rgb(248, 148, 6);">
<?php
foreach ($xml->product->additional->field as $field) {
switch((string) $field['name']) {
case 'price_advice':
echo '€ ' .$field. '';
break;
case 'fromPrice':
echo '€ ' .$field. '';
break;
case 'oldPrice':
echo '€ ' .$field. '';
break;
case 'from_price':
echo '€ ' .$field. '';
break;
case 'adviesprijs':
echo '€ ' .$field. '';
break;
case 'advice_price':
echo '€ ' .$field. '';
break;
case 'Fromprice':
echo '€ ' .$field. '';
break;
case 'recommendedPrice':
echo '€ ' .$field. '';
break;
}
}
?>
</strike>
<p style="color: rgb(153, 153, 153);"><?php echo $desc; ?></p></div>
<div class="btn-group bot"><div class="pull-right"> </div><a class="btn btn-primary" href="<?php echo $xml->product->productURL ;?>">Kopen</a> <a class="btn" href="#<?php echo 'modal_'.get_the_ID();?>">Info</a></div>
</div>
</div>
</li>
<?php /*end loop*/ ?>
To answer your immediate question, consider the following pseudo code:
Check for existence of local file with xml contents. Something like /home/db/tempfiles/feed_name_current_date.xml
If no matching local local file can be found using the construct from step 1, then load the xml as your doing now
After xml has been loaded from URL, save contents using a namespace construct with feed_name and current_date, so when a new request is made, step 1 will return the local file
I’m curious though, if it’s an xml feed, do you not want to store the data in your local system? How do you track purchases etc?