Foros del Web » Programando para Internet » PHP »

me pueden ayudar? la cache funciona pero con este código no

Estas en el tema de me pueden ayudar? la cache funciona pero con este código no en el foro de PHP en Foros del Web. He cogido un código de internet para cachear una pagina en php, es un cache simple. Funciona bien, si cacheo por ejemplo echo "hola"; hace ...
  #1 (permalink)  
Antiguo 05/03/2011, 04:59
 
Fecha de Ingreso: febrero-2011
Ubicación: En Barcelona, españa
Mensajes: 26
Antigüedad: 13 años, 2 meses
Puntos: 1
me pueden ayudar? la cache funciona pero con este código no

He cogido un código de internet para cachear una pagina en php, es un cache simple. Funciona bien, si cacheo por ejemplo echo "hola"; hace el fichero de cache, etc, todo va de maravilla.

include 'cache.php';

$cache1 = new m2Cache();
$cache1->enable();
$cache1->startCache();


aquí pongo lo que quiero cachear y funciona bien.

$cache1->endCache();


Ahora.... el problema viene cuando pongo el script que quiero cachear..... es un script para mostrar rss en la web, funciona tambien bien, pero cuando lo coloco ahi en el medio no me cachea nada.... aunque sigue funcionando bien el script de rss.

Pego aquí el script que pego ahi en el medio y no funciona:


*****************************************

include 'cache.php';

$cache1 = new m2Cache();
$cache1->enable();
$cache1->startCache();

//////lo que quiero cachear///////

define("_NO_SESSION_",0);
require_once("config.inc.php");

$_count = $_GET["count"] ? $_GET["count"] : 10;
$_items = getMyItems();
for($nIndex = 0; $nIndex < count($_xml_sources); $nIndex++)
{
if($_xml_sources[$nIndex]["use"])
{
$_curr_time = mktime(date("H"), date("i"), date("s"), date("m"), date("d"), date("Y"));
if($_curr_time >= $_xml_sources[$nIndex]["next"])
{
$_url = $_xml_sources[$nIndex]["url"];
$_file = _APP_PATH_ . "cache/" . md5($_xml_sources[$nIndex]["url"]);
$_content = implode("",file($_url));
$f = @fopen($_file,"w");
@fwrite($f,$_content);
@fclose($f);
$_url = $_file;
$_diff = $_xml_sources[$nIndex]["next"] - $_xml_sources[$nIndex]["last"];
$_xml_sources[$nIndex]["last"] = $_xml_sources[$nIndex]["next"];
$_xml_sources[$nIndex]["next"] += $_diff;
write_sources();
}
else
{
$_file = _APP_PATH_ . "cache/" . md5($_xml_sources[$nIndex]["url"]);
if(!file_exists($_file))
{
@mkdir(_APP_PATH_ . "cache/");
$_content = implode("",file($_xml_sources[$nIndex]["url"]));
$f = @fopen($_file,"w");
@fwrite($f,$_content);
@fclose($f);
}
$_url = $_file;
}
$_tmp = parseXML($_url);
}
if(count($_xml_data))
$_xml_data = array_merge($_xml_data,$_tmp);
else
$_xml_data = $_tmp;
}
$_xml_data = noDuplicates($_xml_data);
shuffle($_xml_data);
for($nIndex = 0; $nIndex < $_count - count($_items); $nIndex++)
$_all_items[] = $_xml_data[$nIndex];
shuffle($_all_items);
$_all_items = array_merge($_all_items,$_items);
$_output = implode("",file(_APP_PATH_ . "templates/output.inc.php"));
$_content = getContentParts($_output);
$_print = $_content["before"];
for($nIndex = 0; $nIndex < count($_all_items); $nIndex++)
{
$_tmp = $_content["item"];
$_tmp = str_replace("{TITLE}",$_all_items[$nIndex]["title"],$_tmp);
$_tmp = str_replace("{URL}",$_all_items[$nIndex]["url"],$_tmp);
$_tmp = str_replace("{DESCRIPTION}",$_all_items[$nIndex]["description"],$_tmp);
$_print .= $_tmp;
}
$_print .= $_content["after"];
echo $_print;
exit;

function noDuplicates($_array)
{
$_tmp = array();
$_titles = array();
for($nIndex = 0; $nIndex < count($_array); $nIndex++)
{
$_elem = $_array[$nIndex];
if(!in_array($_elem["title"],$_titles))
{
$_tmp[] = $_elem;
$_titles[] = $_elem["title"];
}
}
return $_tmp;
}


function getContentParts($_output)
{
$_pos1 = $_pos2 = 0;
$_pos1 = strpos($_output, "{ITEM}");
if($_pos1)
{
$_pos1 += 6;
$_pos2 = strpos($_output, "{END ITEM}", $_pos1);
if($_pos2)
$_pos2 += 10;
}
if($_pos1 && $_pos2)
{
$_content["before"] = substr($_output,0,$_pos1 - 6);
$_content["item"] = substr($_output,$_pos1,$_pos2 - 10 - $_pos1);
$_content["after"] = substr($_output,$_pos2);
}
else
{
$_output = str_replace("{ITEM}","",$_output);
$_output = str_replace("{END ITEM}","",$_output);
$_content["before"] = "";
$_content["item"] = $_output;
$_content["after"] = "";
}
return $_content;
}

function parseXML($_xml_url)
{
$_content = implode("",file($_xml_url));
if(!$_content)
return;
$p = xml_parser_create();
xml_parse_into_struct($p, $_content, $vals, $index);
xml_parser_free($p);
$_xml = array();
foreach($vals as $_elem)
{
if($_elem["tag"] == "ITEM" && $_elem["type"] == "open")
{
$_element = array();
$_sevent = 1;
$_event = array();
}
if($_elem["tag"] == "ITEM" && $_elem["type"] == "close")
{
$_sevent = 0;
//write item
$_element["title"] = $_event["TITLE"];
$_element["url"] = $_event["LINK"];
$_element["description"] = $_event["DESCRIPTION"];
$_xml[] = $_element;
}
if($_elem["type"] == "complete" && $_sevent)
$_event[$_elem["tag"]] = $_elem["value"];
}
return $_xml;
}

function getMyItems()
{
global $_SETTINGS;

$_items = implode("",file($_SETTINGS["server"]));
$_items = explode("\n###\n",$_items);
$_all = array();
foreach($_items as $_item)
{
$_tmp = explode("\n@@@\n",$_item);
if(count($_tmp) != 3)
continue;
$_elem = array();
$_elem["url"] = $_tmp[0];
$_elem["title"] = $_tmp[1];
$_elem["description"] = $_tmp[2];
$_all[] = $_elem;
}
return $_all;
}

/////////fin de cache////
$cache1->endCache();

?>
*********************************************



gracias
  #2 (permalink)  
Antiguo 07/03/2011, 13:05
Avatar de pateketrueke
Modernizr
 
Fecha de Ingreso: abril-2008
Ubicación: Mexihco-Tenochtitlan
Mensajes: 26.399
Antigüedad: 16 años
Puntos: 2534
Respuesta: me pueden ayudar? la cache funciona pero con este código no

por lo que entiendo, el cache que empleas solo sirve para cachear texto, resultados de un script... ¡no para cachear la ejecución de cierto código!

osea... si dicho script no imprime nada, no se almacena nada... eh ahí el error...
__________________
Y U NO RTFM? щ(ºдºщ)

No atiendo por MP nada que no sea personal.
  #3 (permalink)  
Antiguo 07/03/2011, 14:59
 
Fecha de Ingreso: febrero-2011
Ubicación: En Barcelona, españa
Mensajes: 26
Antigüedad: 13 años, 2 meses
Puntos: 1
Respuesta: me pueden ayudar? la cache funciona pero con este código no

y qué hago para cachear el código ejecutado?
  #4 (permalink)  
Antiguo 07/03/2011, 15:16
 
Fecha de Ingreso: febrero-2011
Ubicación: En Barcelona, españa
Mensajes: 26
Antigüedad: 13 años, 2 meses
Puntos: 1
Respuesta: me pueden ayudar? la cache funciona pero con este código no

me respondo yo solo, hay que iniciar y cerrar la cache en los "echo" que es lo que escupe en el php :)

Etiquetas: cache
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 03:38.