Reputation: 445
I am loading 25000 items (5000 per one sitemap) via DOMDocument from external xml files and it takes about 15 - 20 seconds to loop through the 5 sitemaps in a loop and that 's a lot
I am affraid I am doing something wrong in the loop.
Could you check the code if there is something that is causing the loading to take that long?
I have no clue.
Code:
$resultHTML = '';
$sitemaps = [
'0' => 'http://example.com/sitemap_part1.xml',
'1' => 'http://example.com/sitemap_part2.xml',
'2' => 'http://example.com/sitemap_part3.xml',
'3' => 'http://example.com/sitemap_part4.xml',
'4' => 'http://example.com/sitemap_part5.xml',
];
foreach ( $sitemaps as $sm ) :
$DomDocument = new DOMDocument();
$DomDocument->preserveWhiteSpace = false;
$DomDocument->load($sm);
$DomNodeList = $DomDocument->getElementsByTagName('loc');
foreach($DomNodeList as $url) :
//$i++;
$resultHTML .= '<div class="xml-item">';
$resultHTML .= $url->nodeValue;
$resultHTML .= '</div>';
endforeach;
endforeach;
echo $resultHTML;
Upvotes: 1
Views: 312
Reputation: 2792
This is an untested example how a small file cache could work. You should add some error handling, but I think it will work.
UPDATED: fixed variable names at file_put_contents( $filepath, $resultHTML );
$resultHTML = '';
$chacheDir = "cache";// path/to/your/cachedir
$cacheTime = 24 * 60 * 60;// 24 hours
$sitemaps = [
'0' => 'http://example.com/sitemap_part1.xml',
'1' => 'http://example.com/sitemap_part2.xml',
'2' => 'http://example.com/sitemap_part3.xml',
'3' => 'http://example.com/sitemap_part4.xml',
'4' => 'http://example.com/sitemap_part5.xml',
];
foreach ( $sitemaps as $sm ) :
$filepath = $chacheDir.'/'.md5( $sm );
// check if cached file exists, and if it's too old already
if( file_exists( $filepath ) && ( ( time() - filemtime( $filepath ) ) <= $cacheTime ) ) {
// read from cache
$resultHTML .= file_get_contents( $filepath );
} else {
//create cache file
$DomDocument = new DOMDocument();
$DomDocument->preserveWhiteSpace = false;
//$DomDocument->load($sitemap_url);
$DomDocument->load( $sm );
$DomNodeList = $DomDocument->getElementsByTagName( 'loc' );
foreach ( $DomNodeList as $url ) :
//$i++;
$resultHTML .= '<div class="xml-item">';
$resultHTML .= $url->nodeValue;
$resultHTML .= '</div>';
endforeach;
file_put_contents( $filepath, $resultHTML );
}
endforeach;
echo $resultHTML;
Upvotes: 2