A lot of people want to optimize their code and here is a tips and trick, a solution to cache a part of a dynamic page.
I will take as an example a menu generation. We have a website where we have different categories and subcategories.
So each time, we need to get from database the data, like
$SQL = " SELECT idcategory,$temp FROM ".DB_PREFIX."category WHERE idcategory<>1 AND parent_category=1 AND availability=1 ";
and I will get back for example
Category 1
Category 2
Category 3
etc.
I know that all the time the category is the same, so why I should do this SQL all the time, put in in Fast Template and generate the HTML code, when is much more simple to write it in a file the whole HTML code (ONLY the part for menu) and first I check if that file exist, then show the content. Of course there is a back side of the story, when you add via ADMIN a new category or modify one, to solve this cache problem.
SO, the ideea is to write the whole content of the final Fast Template (the $ft->parse("mainContent", "main"); part).
We write the content, IF file does not exist, into /tmp/menu.html, so in this case first time we will execute the SQL. Then next time, first we verify if that file exist, if not then we generate it, if exist then we just read the content of the file and use it in Fast Template to add to the final page.
When admin modify the category or add new one, we just simply remove the /tmp/menu.html, so next time will be reused the SQL and regenerate the file.
Here is a piece of code (we hope is easy to understand, there is a lot of other things, but you can see the main ideea).
function getMenu($LANG)
{
if(file_exists(INDEX_PATH."tmp/leftmenu.html"))
{
$c = file_get_contents(INDEX_PATH."tmp/leftmenu.html");
}
else
{
$temp = $LANG."_name";
$SQL = " SELECT idcategory,$temp FROM ".DB_PREFIX."category WHERE idcategory<>1 AND parent_category=1 AND availability=1 ";
$SQL = $SQL . " ORDER BY priority ASC;";
$retid = mysql_query($SQL);
if (!$retid) { echo( mysql_error()); }
$i=0;
while ($row = mysql_fetch_array($retid))
{
$idcategory[$i] = $row["idcategory"];
$name[$i] = $row[$temp];
$i++;
}//end while
$nrcategory = $i;
if ($nrcategory != 0)
{
$stringutil = new String("");
$ft = new FastTemplate(TEMPLATE_PATH);
$ft->define(array("main"=>"left_menu.html"));
$ft->define_dynamic("row","main");
for ($i=0;$i<$nrcategory;$i++)
{
$ft->assign("ID_CATEGORY",$idcategory[$i]);
$ft->assign("NAME_CATEGORY",$name[$i]);
$name[$i] = $stringutil->CleanLink($name[$i]);
$cat = new Category(0);
if ($cat->returNrSubcategory ($idcategory[$i])==0)
{
if (NONSEO==1) $ft->assign("PHPNAME", CONF_INDEX_URL."category.php?name=".$name[$i]."&id =".$idcategory[$i]);
else $ft->assign("PHPNAME", CONF_INDEX_URL."category.php/".$name[$i]."/".$idca tegory[$i]."/");
}
else
{
if (NONSEO==1) $ft->assign("PHPNAME", CONF_INDEX_URL."listallsubcategories.php?name=".$n ame[$i]."&id=".$idcategory[$i]);
else $ft->assign("PHPNAME", CONF_INDEX_URL."listallsubcategories.php/".$name[$ i]."/".$idcategory[$i]."/all");
}
$ft->assign("left_menu_subcat.html",$this->ge tSubcategories($idcategory[$i], $LANG));
$ft->parse("ROW",".row");
}// end for
$ft->multiple_assign_define("LANG_");
$ft->multiple_assign_define("CONF_");
$ft->parse("mainContent", "main");
$ft->showDebugInfo(ERROR_DEBUG);
$c = $ft->fetch("mainContent");
}
else
{
$c = LANG_CAT_MSG_NOCATEGORIES_REGISTERED;
}
$filename = INDEX_PATH."tmp/leftmenu.html";
if (!$handle = fopen($filename, 'w+')) {
echo "Cannot open file ($filename)";
exit;
}
// Write $somecontent to our opened file.
if (fwrite($handle, $c) === false) {
echo "Cannot write to file ($filename)";
exit;
}
fclose($handle);
}// else file exist
return $c;
}//end getMenu
//*****************************************
Hope this piece of code will help. |