| View previous topic :: View next topic |
| Author |
Message |
tome Smarty n00b
Joined: 20 Jul 2012 Posts: 4
|
Posted: Fri Jul 20, 2012 1:58 pm Post subject: What is the point of $smarty->isCached?? Any other method |
|
|
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!!!
Please is there anyway around this or an alternative function that works each time the cach has expired.
Many thanks |
|
| Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7061 Location: Lincoln Nebraska, USA
|
Posted: Fri Jul 20, 2012 2:18 pm Post subject: |
|
|
| ?? 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 |
|
rodneyrehm Administrator

Joined: 30 Mar 2007 Posts: 698 Location: Germany, border to Switzerland
|
Posted: Fri Jul 20, 2012 2:22 pm Post subject: Re: What is the point of $smarty->isCached?? Any other me |
|
|
| 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!!! |
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 |
|
tome Smarty n00b
Joined: 20 Jul 2012 Posts: 4
|
Posted: Fri Jul 20, 2012 2:36 pm Post subject: Thanks for response, here is why i'm finding it 'useless' |
|
|
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 |
|
rodneyrehm Administrator

Joined: 30 Mar 2007 Posts: 698 Location: Germany, border to Switzerland
|
Posted: Fri Jul 20, 2012 2:46 pm Post subject: Re: Thanks for response, here is why i'm finding it 'useless |
|
|
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 |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 4199 Location: Hamburg / Germany
|
Posted: Fri Jul 20, 2012 2:47 pm Post subject: |
|
|
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 |
|
tome Smarty n00b
Joined: 20 Jul 2012 Posts: 4
|
Posted: Fri Jul 20, 2012 3:00 pm Post subject: Thanks for reply, got it working now I think |
|
|
please run the $smarty->setCacheLifetime(300); before isCached().
I tried this and it appears to be working now. Its not clear from the docs that you have to setCacheLifetime before isCached.
Many thanks for your help  |
|
| Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7061 Location: Lincoln Nebraska, USA
|
Posted: Fri Jul 20, 2012 3:05 pm Post subject: Re: Thanks for reply, got it working now I think |
|
|
| tome wrote: | please run the $smarty->setCacheLifetime(300); before isCached().
I tried this and it appears to be working now. Its not clear from the docs that you have to setCacheLifetime before isCached.
Many thanks for your help  |
Thats kind of assumed, isn't it? I've never heard this confusion before. |
|
| Back to top |
|
tome Smarty n00b
Joined: 20 Jul 2012 Posts: 4
|
Posted: Fri Jul 20, 2012 5:03 pm Post subject: Still having problems! |
|
|
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 ,
Many thanks |
|
| Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 4199 Location: Hamburg / Germany
|
Posted: Sat Jul 21, 2012 9:04 am Post subject: |
|
|
| 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 |
|
|