Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

isCached method returning empty result
Goto page 1, 2  Next
 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Bugs
View previous topic :: View next topic  
Author Message
demo055
Smarty Rookie


Joined: 08 Apr 2013
Posts: 12

PostPosted: Mon Apr 08, 2013 2:56 pm    Post subject: isCached method returning empty result Reply with quote

i am using smarty file cache with various cache_id, but isCached method is always returning empty result:

Code:

$smarty->setCacheDir('/www/doc/www.myserver.com/www/smarty/cache');
$cache_id = md5($_SERVER['REQUEST_URI']);
$smarty->setCaching(Smarty::CACHING_LIFETIME_SAVED);
$smarty->setCacheLifetime(1209600);
if($smarty->isCached('/www/doc/www.myserver.com/www/graphic/smarty/index.tpl', $cache_id)) {
   $html = $smarty->fetch("/www/doc/www.myserver.com/www/graphic/smarty/index.tpl", $cache_id);
   echo str_replace("<span id=\"cache\"></span>", "from cache", $html);
}
else {
   //do some DB stuff here and smarty vars assignation
   $smarty->display("/www/doc/www.myserver.com/www/graphic/smarty/index.tpl", $cache_id);
}


I can see that smarty is using file cache, but if condition is always false... I was trying to figure it myself, but with no success. please help
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Mon Apr 08, 2013 4:18 pm    Post subject: Reply with quote

Your code lokks correct. It should return $smarty->isCached(...) true for same compile_id's.
Back to top
View user's profile Send private message
demo055
Smarty Rookie


Joined: 08 Apr 2013
Posts: 12

PostPosted: Mon Apr 08, 2013 4:53 pm    Post subject: Reply with quote

well template is successfully created in smarty_internal_templatebase.php, but it looks that, $template->cached->valid is set, but empty string...
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Mon Apr 08, 2013 5:32 pm    Post subject: Reply with quote

If you look manually in the created cache file, do you see content there?
Back to top
View user's profile Send private message
demo055
Smarty Rookie


Joined: 08 Apr 2013
Posts: 12

PostPosted: Thu Apr 11, 2013 8:28 am    Post subject: Reply with quote

Yes, there is a content in cache file. I decided to post whole code. Maybe i am missing something:

Code:
require('libs2/Smarty.class.php');
$smarty = new Smarty;
$smarty->setTemplateDir('/www/doc/www.myserver.com/www/smarty/templates');
$smarty->setCompileDir('/www/doc/www.myserver.com/www/smarty/templates_c');
$smarty->setCacheDir('/www/doc/www.myserver.com/www/smarty/cache');
$smarty->setConfigDir('/www/doc/www.myserver.com/www/smarty/configs');
$cache_id = md5($_SERVER['REQUEST_URI']);
$smarty->setCaching(Smarty::CACHING_LIFETIME_SAVED);
$smarty->setCacheLifetime(1209600);
if($smarty->isCached('/www/doc/www.myserver.com/www/graphic/smarty/index.tpl', $cache_id)) {
   $html = $smarty->fetch("/www/doc/www.myserver.com/www/graphic/smarty/index.tpl", $cache_id);
   echo str_replace("<span id=\"cache\"></span>", "from cache", $html);
}
else {
   //do some DB stuff here and smarty assignation
   $smarty->display("/www/doc/www.myserver.com/www/graphic/smarty/index.tpl", $cache_id);
}
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Thu Apr 11, 2013 3:07 pm    Post subject: Reply with quote

Why are you using a directory different than your template_dir?

/www/doc/www.myserver.com/www/smarty/templates

vs

/www/doc/www.myserver.com/www/graphic/smarty/


You should set your template_dir, then use paths relative to that in fetch() and display()
Back to top
View user's profile Send private message Visit poster's website
demo055
Smarty Rookie


Joined: 08 Apr 2013
Posts: 12

PostPosted: Thu Apr 11, 2013 3:39 pm    Post subject: Reply with quote

well i also tried to set template directory to /www/doc/www.myserver.com/www/graphic/smarty/ and then used relative paths, but smarty wasn't possible to locate my template:

Code:
$smarty->setTemplateDir('/www/doc/www.myserver.com/www/graphic/smarty');
$smarty->fetch("index.tpl", $cache_id);
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Thu Apr 11, 2013 3:49 pm    Post subject: Reply with quote

run the install test after you setup your directories.

$smarty->testInstall();


Last edited by mohrt on Thu Apr 11, 2013 3:55 pm; edited 1 time in total
Back to top
View user's profile Send private message Visit poster's website
U.Tews
Administrator


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

PostPosted: Thu Apr 11, 2013 3:54 pm    Post subject: Reply with quote

This is strange...

Call $smarty->testInstall(); after you did set all directories. It should show if there are any access/permission problems.
Back to top
View user's profile Send private message
demo055
Smarty Rookie


Joined: 08 Apr 2013
Posts: 12

PostPosted: Mon Apr 22, 2013 8:03 pm    Post subject: Reply with quote

ok, sorry for the delayed response i was on a business trip. This is testInstall output:
Code:
Smarty Installation test...
Testing template directory...
/www/doc/www.myserver.com/www/graphic/smarty is OK.
Testing compile directory...
/www/doc/www.myserver.com/www/smarty/templates_c is OK.
Testing plugins directory...
/www/doc/www.myserver.com/www/companies/libs2/plugins is OK.
Testing cache directory...
/www/doc/www.myserver.com/www/smarty/cache is OK.
Testing configs directory...
/www/doc/www.myserver.com/www/smarty/configs is OK.
Testing sysplugin files...
... OK
Testing plugin files...
... OK
Tests complete.
Everything seems to be okay but isCached method is still returning empty result. I am desperate.
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Sun Apr 28, 2013 6:48 pm    Post subject: Reply with quote

Are there any files in your cache dir?
Back to top
View user's profile Send private message Visit poster's website
demo055
Smarty Rookie


Joined: 08 Apr 2013
Posts: 12

PostPosted: Sun Apr 28, 2013 7:58 pm    Post subject: Reply with quote

well i am using memcache now. But when i switch to standard file cache - yes there is a non-empty files in my cache directory. But method is not working... but i must emphasize that i didn't make any changes to original file of newest stable smarty version...
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Sun Apr 28, 2013 8:21 pm    Post subject: Reply with quote

Quote:
Everything seems to be okay but isCached method is still returning empty result


Not that isCached does not return content. It returns just true/false to indicate if the cache file is valid or not.
You must still call display() or fetch() to get the content.
Back to top
View user's profile Send private message
demo055
Smarty Rookie


Joined: 08 Apr 2013
Posts: 12

PostPosted: Sun Apr 28, 2013 8:30 pm    Post subject: Reply with quote

Quote:
Not that isCached does not return content. It returns just true/false to indicate if the cache file is valid or not.
You must still call display() or fetch() to get the content.


I know that. I just don't want to call DB stuff, when page is cached. It should return true or false, but it is returning empty string. When i pass the result to PHP "empty" function it returns true.

So isCached returns neither true nor false, just empty string.
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Sun Apr 28, 2013 9:52 pm    Post subject: Reply with quote

isCached() can't return an empty string. It will always return an boolean.

I have verified the code and it does work in thousand's of application.

That's really strange. Which veerion of Smarty do you use?
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Bugs All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
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