0

I am generating an xml file which takes way too long to be generated and this is a big problem for me because some xml integrations are not working due to the delay time.

I want to cache an xml copy of this xml file, but i dont know how to do this, below is the code that generates the xml:

<?php
defined('_JEXEC') or die( 'Restricted access' );

jimport( 'joomla.application.component.view');

class PropertiesViewProperties extends JView
{
function display($tpl = null)
{   
    global $mainframe;
    $db         =& JFactory::getDBO();

$query = 'SELECT p.*,c.name as name_category,t.name as name_type,cy.name as name_country,s.name as name_state,l.name as name_locality,l.alias as locality_alias,pf.name as name_profile, '
. ' CASE WHEN CHAR_LENGTH(p.alias) THEN CONCAT_WS(":", p.id, p.alias) ELSE p.id END as Pslug,'
. ' CASE WHEN CHAR_LENGTH(c.alias) THEN CONCAT_WS(":", c.id, c.alias) ELSE c.id END as Cslug,'
. ' CASE WHEN CHAR_LENGTH(cy.alias) THEN CONCAT_WS(":", cy.id, cy.alias) ELSE cy.id END as CYslug,'
. ' CASE WHEN CHAR_LENGTH(s.alias) THEN CONCAT_WS(":", s.id, s.alias) ELSE s.id END as Sslug,'      
. ' CASE WHEN CHAR_LENGTH(l.alias) THEN CONCAT_WS(":", l.id, l.alias) ELSE l.id END as Lslug, ' 
. ' CASE WHEN CHAR_LENGTH(t.alias) THEN CONCAT_WS(":", t.id, t.alias) ELSE t.id END as Tslug '          
    . ' FROM #__products AS p '              
    . ' LEFT JOIN #__country AS cy ON cy.id = p.cyid '               
    . ' LEFT JOIN #__state AS s ON s.id = p.sid '
    . ' LEFT JOIN #__locality AS l ON l.id = p.lid '
    . ' LEFT JOIN #__profiles AS pf ON pf.mid = p.agent_id '             
    . ' LEFT JOIN #__category AS c ON c.id = p.cid '
    . ' LEFT JOIN #__type AS t ON t.id = p.type '
    . ' WHERE p.published = 1 '
    . ' AND p.available <> 2 '
    .' ORDER BY p.id desc limit 10, 999999' 
    ;


            $db->setQuery( $query );                
            $this->products = $db->loadObjectList();

$document = &JFactory::getDocument();
$document->setMimeEncoding('text/xml');     
//header ("content-type: text/xml");

switch(JRequest::getVar('feed'))
    {
    case 'todobrasilimobiliaria' :  
    $this->todobrasilimobiliaria();
    break;

    }

}




function Images($id)
{       
$db     =& JFactory::getDBO();  
$query = ' SELECT i.* '         
        . ' FROM #__images as i '                   
        . ' WHERE i.published = 1 AND i.parent = '.$id          
        . ' order by i.ordering ';      
    $db->setQuery($query);
    $Images = $db->loadObjectList();
return $Images;
}

function todobrasilimobiliaria()
{

$todobrasilimobiliaria_cat_id = array();
$todobrasilimobiliaria_cat_id[17]='apartamento padrão';
$todobrasilimobiliaria_cat_id[18]='casa';
$todobrasilimobiliaria_cat_id[19]='casa em condomínio';
$todobrasilimobiliaria_cat_id[20]='cobertura';
$todobrasilimobiliaria_cat_id[21]='área';
$todobrasilimobiliaria_cat_id[22]='comercial';
$todobrasilimobiliaria_cat_id[25]='apartamento padrão';
$todobrasilimobiliaria_cat_id[26]='terreno em condomínio';
$todobrasilimobiliaria_cat_id[27]='pousada';
$todobrasilimobiliaria_cat_id[28]='sítio';
$todobrasilimobiliaria_cat_id[29]='terreno';


$crlf="\n";
$line =  '<?xml version="1.0" encoding="utf-8"?>' . $crlf;  
$line .=  '<root>'. $crlf;
$line .=  '<todobrasil>'. $crlf;
$line .=  '<feed_version>2_1</feed_version>'. $crlf;
$line .=  '</todobrasil>'. $crlf;


    foreach ( $this->products as $row )
    {               
        $url = substr(JURI::base(), 0, -1);     
        $link = $url.LinkHelper::getLink('properties','showproperty','',$row->CYslug,$row->Sslug,$row->Lslug,$row->Cslug,$row->Tslug,$row->Pslug);

        $title = $this->escape( $row->name );           
        $category = 'vender';
        $tipo = $todobrasilimobiliaria_cat_id[$row->cid];           
        $data_entrega = '00/00/0000';
        $estado_superachei = 'SC';
        $type = $row->name_type;
        $desc = $row->text;         
        $imageBaseUrl = JURI::root().'images/';     
        $imageUrl = $imageBaseUrl.$row->id.'/';
        $images=$this->Images($row->id);    
        $listdate = JFactory::getDate($row->refresh_time)->toFormat('%d/%m/%Y');
        $chamada = trim($row->name_category, 's').' tipo '.$row->name_type.'  - '.$row->name_locality;
$line .=  '<property>'. $crlf;  
$line .='   <id><![CDATA['.$row->id.']]></id>' . $crlf;     
$line .='   <date><![CDATA['.$row->refresh_time.']]></date>' . $crlf;       
$line .='   <ref><![CDATA['.$row->ref.']]></ref>' . $crlf;      
$line .='   <price><![CDATA['.(int)$row->price.']]></price>' . $crlf;   
$line .='   <price_freq>venda</price_freq>' . $crlf;    
$line .=  '<type>'. $crlf;  
$line .='   <en><![CDATA['.$tipo.']]></en>' . $crlf;        
$line .=  '</type>'. $crlf; 
$line .='   <country>Brasil</country>' . $crlf; 
$line .='   <currency>BRL</currency>' . $crlf;  
$line .='   <town><![CDATA['.$row->name_state.']]></town>' . $crlf;     
$line .='   <province><![CDATA['.$row->name_country.']]></province>' . $crlf;       
$line .='   <location_detail><![CDATA['.$row->name_locality.']]></location_detail>' . $crlf;        

if($row->bedrooms>0){   
$line .='   <beds><![CDATA['.$row->bedrooms.']]></beds>' . $crlf;   
}
if($row->bathrooms>0){  
$line .='   <baths><![CDATA['.$row->bathrooms.']]></baths>' . $crlf;    
}   

$line .='   <url><![CDATA['.$link.']]></url>' . $crlf;

$line .=  '<surface_area>'. $crlf;  
$line .='   <built><![CDATA['.$row->covered_area.']]></built>' . $crlf;
$line .='   <plot><![CDATA['.$row->area.']]></plot>' . $crlf;
    $line .=  '</surface_area>'. $crlf; 
$line .=  '<desc>'. $crlf;  
$line .='   <en><![CDATA['.strip_tags($row->text).']]></en>' . $crlf;
$line .=  '</desc>'. $crlf; 


$line .='<images>' . $crlf; 
$i=0;
foreach($images as $img) {
if ($i < 10) 
{
    $i +=1;
    $line .= '   <image id="'.$i.'">' . $crlf; 
    $line .='   <url><![CDATA['.$imageUrl.$img->name.']]></url>' . $crlf;   
    $line .= '   </image>' . $crlf;   
} 
}  
$line .='</images>' . $crlf;  

$line .=  '</property>'. $crlf; 
    }
$line .=  '</root>'. $crlf; 
echo $line;     
}

I would like to speed up the xml generation because it takes more than 2 minutes to generate and i am on a dedicated server, so the solution I have in mind is to cache a copy of this, anyone can help me?

4

1 回答 1

0

Joomla 几乎可以缓存任何东西:您应该使用帮助程序中的静态方法 getList() 检索查询结果,然后使用适当的 JCache 后代:这是针对模块的:

$cacheparams = new stdClass;
$cacheparams->cachemode = 'safeuri';
$cacheparams->class = 'yourclassHelper';
$cacheparams->method = 'getList';
$cacheparams->methodparams = $params;
$cacheparams->modeparams = array('someparam'=>'int');

$list = JModuleHelper::moduleCache ($module, $params, $cacheparams);

确保 modeparams 仅包含会使查询发生变化的参数,以确保查询不会运行得太频繁。

另外,删除 $mainframe 我猜它来自旧版本的组件。

最后,您是否正确索引要加入的表?即,您是否为要加入的每个列创建了索引?索引使数据库查询飞起来。

于 2013-08-27T05:54:51.270 回答