Forums Sign Up Reply Search Statistics
Fast Template GraFX Software Solutions - Forum / Fast Template /

Possible Cache bug?

 
Author rr1024
Forums Member
#1 Posted: 21 Nov 2007 19:38:38
Reply 
I'm not sure but there seems to be a cache bug or maybe I'm not using the cache correctly.


I applied Cache to your example2.php where you specify three files for parsing.
$ft->define(
array(
'header' => "header.html",
'body' => "middle.html",
'footer' => "footer.html"
)
);
AND
$ft->parse('HEAD', "header");
$ft->parse('BODY', "body");
$ft->parse('FOOT', "footer");

However, when I view the file example2.php the first instance looks fine which is when the cache gets created. When I refresh the page to view the cached version I will only get the html output from either the header, body or footer randomly.

In other words the cache appears to create the cache only for 1 of the three templates being used and not all three files combined.

I will try to play around with it in case it's something I'm doing wrong but you may want to check this.
Author rr1024
Forums Member
#2 Posted: 21 Nov 2007 20:09:19
Reply 
Ok I think I confirmed this as a bug, the random cache was solved by moving the cache futher up in the script.

Now the only thing being cached is the footer, viewing the source of the cache file .ft extention verifies that only the html from the footer gets cached.
Author rr1024
Forums Member
#3 Posted: 21 Nov 2007 20:23:02
Reply 
Looking at the code and I am not experienced in working with classes yet but it seems there needs to be away to tell cache_file function that there are more files that make up this cache. It should probably work or run off the parser, by saving the parse of each file i.e. header, body, footer to seperate vars and then combining the vars to generate the cache
Author rr1024
Forums Member
#4 Posted: 21 Nov 2007 21:56:16
Reply 
I was thinking and looking over the code, it seems this could be done but will change how the cache is done.

If cache is enabled and you tell the script to use cache then pass a default file name with use cache something like
$ft->USE_CACHE(myfile.php);

This would be the base name for md5 of cache filename, then to generate cache file it should parse the array list together such as in parse() or somewhere else that makes sense maybe in getfast()

basically need a way to detect that there are more than one templates being called then when we call
IF ( $this->USE_CACHE) {
$this->cache_file($this->$template);

then we need a foreach loop to compile all required templates into the md5(myfile.php).ft blablabla

I think
Author software
Admin
#5 Posted: 22 Nov 2007 13:23:17
Reply 
Cache is designed for only 1 file ( the final). If you want to use partial caches you can always use 2 ft and fetch one into another.
Author rr1024
Forums Member
#6 Posted: 23 Nov 2007 05:02:53
Reply 
What I would like to do is have the cache detect a multi template config, because the I may use slightly different headers i.e. <head> </head> with different meta tags etc... i.e. one page many be about
Electrostatic discharge while another page maybe about some totally different subject. with different meta tages and discription / title tags.


so is there there a away to detect that the templates contain more that one element in the array and if so would it be possible to let say in
currently available:
FUNCTION cache_file ( $content = "" )
{
IF ( ($this->USE_CACHE) && (!$this->cache_file_is_updated())) {
IF ( !$this->CACHING) {
$fname = $this->self_script_in_cache_path();
}ELSE{
$fname = $this->CACHING;
}

$fname = $fname.".ft";
// Tendo certeza que o arquivo existe e que há permissão de escrita primeiro.
//IF ( is_writable($fname)) {
// Opening $fname in writing only mode

IF ( !$CACHE_MULTI ){

IF ( !$fp = fopen($fname, 'w')) {
$this->error("Error WHILE opening cache file ($fname)",0);
RETURN;
}
// Writing $content to open file.
IF ( !fwrite($fp, $content)) {
$this->error("Error WHILE writing cache file ($fname)",0);
RETURN;
}ELSE{
fclose($fp);
include $fname;
RETURN;
}
}

changed to something like

FUNCTION cache_file ( $content = "" )
{
IF ( ($this->USE_CACHE) && (!$this->cache_file_is_updated())) {
IF ( !$this->CACHING) {
$fname = $this->self_script_in_cache_path();
}ELSE{
$fname = $this->CACHING;
}

$fname = $fname.".ft";
// Tendo certeza que o arquivo existe e que há permissão de escrita primeiro.
//IF ( is_writable($fname)) {
// Opening $fname in writing only mode

IF ( !$CACHE_MULTI ){

IF ( !$fp = fopen($fname, 'w')) {
$this->error("Error WHILE opening cache file ($fname)",0);
RETURN;
}
// Writing $content to open file.
IF ( !fwrite($fp, $content)) {
$this->error("Error WHILE writing cache file ($fname)",0);
RETURN;
}ELSE{
fclose($fp);
include $fname;
RETURN;
}
}ELSE{
IF ( !$fp = fopen($fname, 'a')) {
$this->error("Error WHILE opening cache file ($fname)",0);
RETURN;
}
// Writing $content to open file.
IF ( !fwrite($fp, $content)) {
$this->error("Error WHILE writing cache file ($fname)",0);
RETURN;
}ELSE{
fclose($fp);
include $fname;
RETURN;
}
}
fclose($fp);
//} ELSE {
// $this->error("The cache file $fname is not writable",0);
// RETURN;
//}
}
} // end cache_file()

I think something like this should work but not sure, a new var would be needed for $CACHE_MULTI and open the file in append mode....


Sorry I just don't know what the best approach is because I'm new to the whole class thing...I believe in fast and effiecient code so I just don't know which appoache works best for classes.

I think with the exsisting code it is available to adapt with some good forthought but need help in thinking correctly for the proper appoach.
Author rr1024
Forums Member
#7 Posted: 24 Nov 2007 08:38:03
Reply 
This almost works and caches header, body and footer templates, however there is one small issue which I can't figure out yet.

When the cache is updated it is shown twice for some reason but the cache file it self is correct containing only one instance of header, body and footer.
in test file I have
$TE = new FastTemplate( $PageParams['TE_DIR_TPL'] );

$TE->define(
array(
'header' => "header.html",
'body' => $PageParams['TE_HTM_BODY'],
'footer' => "footer.html"
)
);
IF ( $PageParams['TE_ENB_CACHE'] == TRUE ){
$TE->CACHE_MULTI = 3;
$TE->CACHE_PATH = $PageParams['TE_DIR_TPL']."cache/"; #Cache directory
$TE->setCacheTime( $PageParams['TE_TME_CACHE'] ); #Cache Duration, how long in seconds
//$TE->$CACHE_URI = $_SERVER['REQUEST_URI'];
$TE->USE_CACHE();
}

Updated fast template class
Added these two vars

VAR $CACHE_MULTI = FALSE;//$CACHE_MULTI set to number of files to be combined into one cache.
VAR $CACHE_MULTI_CNT = 0; //private used only for counting in cache file function.

Changed cache_file function
FUNCTION cache_file( $content = "" )
{
$RW = 'w';#PROTOTYPE

IF ( ($this->USE_CACHE) && (!$this->cache_file_is_updated())) {
IF ( !$this->CACHING) {
$fname = $this->self_script_in_cache_path();
}ELSE{
$fname = $this->CACHING;
}
$fname = $fname.".ft";
// Tendo certeza que o arquivo existe e que há permissão de escrita primeiro.
//IF ( is_writable($fname)) {
// Opening $fname in writing only mode


IF( $this->CACHE_MULTI > 1 ) { #PROTOTYPE

IF ( $this->CACHE_MULTI_CNT == 0 ){ //new cache
$RW = 'w';
}ELSE{
$RW = 'a';
}

$this->CACHE_MULTI_CNT++;

IF ( $this->CACHE_MULTI_CNT === $this->CACHE_MULTI ){
$this->CACHE_MULTI_CNT = 0;
}
}#END PROTOTYPE

//$this->error("Cache file Count: ( $this->CACHE_MULTI_CNT )<BR>Cache File Name: ( $fname )<BR>Cache Mult= ( $this->CACHE_MULTI )",0);

IF ( !$fp = fopen($fname, $RW)) {
$this->error("Error WHILE opening cache file ($fname)",0);
RETURN;
}
// Writing $content to open file. $this->CACHE_MULTI_CNT
$content = $content."from cache file";
IF ( !fwrite($fp, $content)) {
$this->error("Error WHILE writing cache file ($fname)",0);
RETURN;
}ELSE{
fclose($fp);
include $fname;
RETURN;
}

//$this->error("Error WHILE writing cache file ($fname) <br>Count = ($CACHE_MULTI_CNT) <br>cachmulti=($CACHE_MULTI)",0);
//ECHO $CACHE_MULTI_CNT;
//echo "The file $fname exists";
// echo "444Increase= ".$CACHE_MULTI_CNT."<br />";
// echo "444howmany= ".$CACHE_MULTI."<br />";

fclose($fp);
//} ELSE {
// $this->error("The cache file $fname is not writable",0);
// RETURN;
//}
// echo "Increase= ".$CACHE_MULTI_CNT."<br />";
// echo "howmany= ".$CACHE_MULTI."<br />";
}
}

I think this will be a great mod but I need a little help, like I said not much experience in classes so I don't understand why when the cache is updated it's displays the cache file twice......Like I said the cache file is being writen correctly.
Author rr1024
Forums Member
#8 Posted: 25 Nov 2007 01:58:01
Reply 
Ok got it working and tested it with a example 1, 2 and 3 all those worked just fine.
This will allow multiple caches to be rolled into one cache file, rather than cache a seperate header, body and footer it will cache all three into one cache file.

You may have to test it with all your examples

NOTE: this with with my mode where I removed the trailing slash

Added these three vars


VAR $CACHE_MULTI = FALSE;//$CACHE_MULTI number of files to cache. if you have defined header.html, body.html and footer.html then this will be set to 3
VAR $CACHE_MULTI_CNT = 0; Used to count iterations
VAR $CACHE_COMPLETE = FALSE; // set to true when all files have been cached.


FUNCTION cache_file( $content = "" )
{
$RW = 'w';
$Temp = $this->cache_file_is_updated();
//$this->error("USE_CACHE = ( $this->USE_CACHE )<BR>cache_file_is_updated: ( $Temp )",0);
IF ( ($this->USE_CACHE) OR (!$this->cache_file_is_updated())) {
IF ( !$this->CACHING) {
//$this->error("Cache file self_script_in_cache_path)",0);
$fname = $this->self_script_in_cache_path();
}ELSE{
//$this->error("Cache file CACHING",0);
$fname = $this->CACHING;
}
$fname = $fname.".ft";
// Tendo certeza que o arquivo existe e que há permissão de escrita primeiro.
//IF ( is_writable($fname)) {
// Opening $fname in writing only mode


IF( $this->CACHE_MULTI > 1 ) {

IF ( $this->CACHE_MULTI_CNT == 0 ){ //new cache
$RW = 'w';
}ELSE{
$RW = 'a';
}

$this->CACHE_MULTI_CNT++;
//$this->error("Cache file Count: ( $this->CACHE_MULTI_CNT )<BR>RW: ( $RW )",0);
IF ( $this->CACHE_MULTI_CNT === $this->CACHE_MULTI ){
$this->CACHE_MULTI_CNT = 0;
$this->CACHE_COMPLETE = TRUE;
}
}

IF ( !$fp = fopen($fname, $RW)) {
$this->error("Error WHILE opening cache file ($fname)",0);
RETURN;
}
// Tendo certeza que o arquivo existe e que há permissão de escrita primeiro.
//IF ( is_writable($fname)) {
// Opening $fname in writing only mode
IF ( !fwrite($fp, $content) ) {
$this->error("Error WHILE writing cache file ($fname)",0);
RETURN;
}ELSE{
fclose($fp);
IF ( $this->CACHE_COMPLETE OR $this->CACHE_MULTI <= 1 ){ #use include cache upon completed or single page cache
include $fname;
}
RETURN;
}
//$this->error("Error WHILE writing cache file ($fname) <br>Count = ($CACHE_MULTI_CNT) <br>cachmulti=($CACHE_MULTI)",0);
fclose($fp);
//} ELSE {
// $this->error("The cache file $fname is not writable",0);
// RETURN;
//}

}

//------------------------------------------------ -------------------------
/*
IF ( ($this->USE_CACHE) && (!$this->cache_file_is_updated())) {
IF ( !$this->CACHING) {
$fname = $this->self_script_in_cache_path();
}ELSE{
$fname = $this->CACHING;
}

$fname = $fname.$this->CACHE_EXT;
// Tendo certeza que o arquivo existe e que há permissão de escrita primeiro.
//IF ( is_writable($fname)) {
// Opening $fname in writing only mode
IF ( !$fp = fopen($fname, 'w')) {
$this->error("Error WHILE opening cache file ($fname)",0);
RETURN;
}
// Writing $content to open file.
IF ( !fwrite($fp, $content)) {
$this->error("Error WHILE writing cache file ($fname)",0);
RETURN;
}ELSE{
fclose($fp);
include $fname;
RETURN;
}

fclose($fp);
//} ELSE {
// $this->error("The cache file $fname is not writable",0);
// RETURN;
//}
}
*/
//------------------------------------------------ ------------------------
} // end cache_file()
Author software
Admin
#9 Posted: 8 Dec 2007 19:05:12
Reply 
A lot of code :-) hope will help someone who use FT
 
Your Reply

» Username  » Password 
Only registered users are allowed to post here. Please enter your login/password details upon posting a message, or sign up first.