简体   繁体   English

如何在PHP中使用DOMDocument加快循环

[英]How to speed up loop using DOMDocument in PHP

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 'sa lot 我通过DOMDocument从外部xml文件加载25000个项(每个站点地图5000个),并且在一个循环中循环浏览5个站点地图大约需要15-20秒。

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;

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 ); 更新:file_put_contents( $filepath, $resultHTML );处的固定变量名称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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM