Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.
Dedicated server web hosting provided by Guru-host.eu.
What is the point of $smarty->isCached?? Any other method

 
Post new topic   Reply to topic    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
tome
Smarty n00b


Joined: 20 Jul 2012
Posts: 4

PostPosted: Fri Jul 20, 2012 1:58 pm    Post subject: What is the point of $smarty->isCached?? Any other method Reply with quote

I tried using $smarty->isCached to see whether a template is cached and so only execute database code if it returns false. The problems is according to the manual technical Note:

http://www.smarty.net/docs/en/api.is.cached.tpl

is that it stores its own cache internally, this means it can only be used once which basically makes it a useless function!!! Evil or Very Mad

Please is there anyway around this or an alternative function that works each time the cach has expired.

Many thanks
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7061
Location: Lincoln Nebraska, USA

PostPosted: Fri Jul 20, 2012 2:18 pm    Post subject: Reply with quote

?? The internally cached file is only for the current script execution to avoid race conditions. Is it not behaving as you expect?
Back to top
View user's profile Send private message Visit poster's website
rodneyrehm
Administrator


Joined: 30 Mar 2007
Posts: 698
Location: Germany, border to Switzerland

PostPosted: Fri Jul 20, 2012 2:22 pm    Post subject: Re: What is the point of $smarty->isCached?? Any other me Reply with quote

tome wrote:
is that it stores its own cache internally, this means it can only be used once which basically makes it a useless function!!! Evil or Very Mad


please elaborate on the "is useless" part.

The note in the docs translates to "once this function is called for a certain template, that state is kept in memory. So calling the function again (within the same script execution context) will not re-check the cache availabilty, but serve that info from memory" - I fail to see how this is "useless".
_________________
Twitter
Back to top
View user's profile Send private message Visit poster's website
tome
Smarty n00b


Joined: 20 Jul 2012
Posts: 4

PostPosted: Fri Jul 20, 2012 2:36 pm    Post subject: Thanks for response, here is why i'm finding it 'useless' Reply with quote

Hi guys, thanks for replies. The reason I'm finding it 'useless' is because I have a simple script like this:

require_once('Smarty.class.php');
$smarty = new Smarty;

$smarty->setCaching(Smarty::CACHING_LIFETIME_SAVED);

if(!$smarty->isCached('mytemplate.tpl')) { // not cached
$smarty->assign("myvar",'assigned!!',true); //myvar would come from lots of database calls which I do not want repeated until the cach expires when I want them called again

} else { // cached


}


$smarty->setCacheLifetime(300); // cache expires regularly
$smarty->display('mytemplate.tpl');


'mytemplate.tpl' is a simple template with {$myvar} only

But since isCached only works once, when the cache expires my database calls don't get run again!

Many thanks
Back to top
View user's profile Send private message
rodneyrehm
Administrator


Joined: 30 Mar 2007
Posts: 698
Location: Germany, border to Switzerland

PostPosted: Fri Jul 20, 2012 2:46 pm    Post subject: Re: Thanks for response, here is why i'm finding it 'useless Reply with quote

please run the $smarty->setCacheLifetime(300); before isCached().

tome wrote:
But since isCached only works once, when the cache expires my database calls don't get run again!


Have you tried it? I think you misunderstood the docs. isCached() does not work only once. It works once *per script execution context*. That means once for every time you run your script. If you run that script 100 times, the cache is checked 100 times.

What the note was supposed to tell you is

Code:
// testing if cache is valid and storing state in memory
$smarty->isCached("some.tpl");

// oh look, we already tested that template and have its state in memory, so don't bother testing again, for you know, performance and useless stuff like that.
$smarty->isCached("some.tpl");

_________________
Twitter
Back to top
View user's profile Send private message Visit poster's website
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 4199
Location: Hamburg / Germany

PostPosted: Fri Jul 20, 2012 2:47 pm    Post subject: Reply with quote

isCache() will return false every time the cache has expired.

As rodney said if you use isCache() for same template within one script run it will return same result for all calls.
Back to top
View user's profile Send private message
tome
Smarty n00b


Joined: 20 Jul 2012
Posts: 4

PostPosted: Fri Jul 20, 2012 3:00 pm    Post subject: Thanks for reply, got it working now I think Reply with quote

please run the $smarty->setCacheLifetime(300); before isCached().

I tried this and it appears to be working now. Very Happy Its not clear from the docs that you have to setCacheLifetime before isCached. Sad

Many thanks for your help Very Happy
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7061
Location: Lincoln Nebraska, USA

PostPosted: Fri Jul 20, 2012 3:05 pm    Post subject: Re: Thanks for reply, got it working now I think Reply with quote

tome wrote:
please run the $smarty->setCacheLifetime(300); before isCached().

I tried this and it appears to be working now. Very Happy Its not clear from the docs that you have to setCacheLifetime before isCached. Sad

Many thanks for your help Very Happy


Thats kind of assumed, isn't it? I've never heard this confusion before.
Back to top
View user's profile Send private message Visit poster's website
tome
Smarty n00b


Joined: 20 Jul 2012
Posts: 4

PostPosted: Fri Jul 20, 2012 5:03 pm    Post subject: Still having problems! Reply with quote

Having problems with this script:

<?php
ini_set('display_errors',1);
require_once('libs/Smarty.class.php');
$smarty = new Smarty;


$smarty->setCaching(Smarty::CACHING_LIFETIME_SAVED);

$smarty->setCacheLifetime(3);
if(!$smarty->isCached('mytemplate.tpl')) {
echo 'not cached';
// lots of database calls would go here! and would be assigned to $mydate
$mydate = gmdate("Y-m-d H:i:s");
$smarty->assign("myvar",$mydate,true);
} else {
echo 'cached';
$mydate = 'xxx';
$smarty->assign("myvar",$mydate,true);
}



$smarty->display('mytemplate.tpl');

?>

contents of mytemplate.tpl:

<div>
{$myvar}
</div>

I want the template to cache but if I assign myvar in the else clause the template doesn't cache and If I don't assign I get a php notice. The point of the isCached is so that database calls are only called when the cache expires.

Need help Sad ,

Many thanks
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 4199
Location: Hamburg / Germany

PostPosted: Sat Jul 21, 2012 9:04 am    Post subject: Reply with quote

Quote:
$smarty->assign("myvar",$mydate,true);


The 3rd paramter 'true' tells Smarty not to cache the value. That's why you got the notice as no dynamic value was assigned when the page was cached.


Code:
<?php
ini_set('display_errors',1);
require_once('libs/Smarty.class.php');
$smarty = new Smarty;


$smarty->setCaching(Smarty::CACHING_LIFETIME_CURRENT);

$smarty->setCacheLifetime(3);
if(!$smarty->isCached('mytemplate.tpl')) {
echo 'not cached';
// lots of database calls would go here! and would be assigned to $mydate
$mydate = gmdate("Y-m-d H:i:s");
$smarty->assign("myvar",$mydate,false);   <----------------------
} else {
echo 'cached';
}



$smarty->display('mytemplate.tpl');

?>


I changed also the code to CACHING_LIFETIME_CURRENT. Otherwise you will not see a change of the cache lifetime immedeately. With CACHING_LIFETIME_SAVED the new value is used only when the cache expires with the old value.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP