如何在JOOMLA中缓存PHP生成的XML文件?

时间:2013-08-26 15:52:02

标签: xml caching joomla

我正在生成一个xml文件,生成时间太长,这对我来说是个大问题,因为由于延迟时间,某些xml集成无法正常工作。

我想缓存此xml文件的xml副本,但我不知道如何执行此操作,下面是生成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;     
}

我想加速xml生成,因为它需要超过2分钟才能生成并且我在专用服务器上,所以我想到的解决方案是缓存这个副本,任何人都可以帮助我吗? / p>

1 个答案:

答案 0 :(得分: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,我猜它是旧版本的组件。

最后,你是否正确地索引了你加入的牌桌?即,您为要加入的每个列创建索引?索引使数据库查询飞行。