Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.
[RESOLVED] Issue with SetCacheDir()

 
Post new topic   Reply to topic    Smarty Forum Index -> Bugs
View previous topic :: View next topic  
Author Message
bluejester
Smarty Regular


Joined: 26 Apr 2012
Posts: 55

PostPosted: Thu Jan 10, 2013 4:35 pm    Post subject: [RESOLVED] Issue with SetCacheDir() Reply with quote

Hello,

I'm having an issue with the SetCacheDir() method. (Smarty 3.1.12)

Example code :

Code:

$smarty->setCaching(Smarty::CACHING_LIFETIME_CURRENT);
$smarty->setCacheLifeTime(1800);
$smarty->setCacheId('some_cache_id');

$smarty->setCacheDir('/var/www/somedir');
echo $smarty->getCacheDir(); // correctly outputs /var/www/somedir/

# Now we want to cache in a subfolder since there could be too many files in somedir with high traffic websites

$today = date('Y-m-d');
$user = 'bluejester';

$smarty->setCacheDir($smarty->getCacheDir() . $user . DS . $today);
echo $smarty->getCacheDir(); // correctly outputs /var/www/somedir/bluejester/2013-01-10/

$smarty->fetch('SomeTPL.tpl', $smarty->getCacheId()); // caches into /var/www/somedir/ instead of /var/www/somedir/bluejester/2013-01-10/


Any idea ?

Thanks,
Ben

PS : the reason I don't set /var/www/somedir/bluejester/2013-01-10/ as cache dir at first is because I want the clearCache function to work with /var/www/somedir/ and any of its subfolders.


Last edited by bluejester on Fri Jan 11, 2013 2:51 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: 4683
Location: Hamburg / Germany

PostPosted: Thu Jan 10, 2013 6:27 pm    Post subject: Reply with quote

Did you access template 'SomeTPL.tpl' in your script also before you did set the cache dir to the new value?

Smarty does cache the template object on the first access of a template. If you change the cache dir afterwards this is not seen by the cached template object.

You can clear the template cache manually by

$smarty->template_objects = array();
Back to top
View user's profile Send private message
bluejester
Smarty Regular


Joined: 26 Apr 2012
Posts: 55

PostPosted: Thu Jan 10, 2013 8:12 pm    Post subject: Reply with quote

No, I didn't, there's only one fetch() called and I setCacheDir() the line before.

I'm not coding in procedural mode but in object mode ; I'll check tomorrow if the method calling the setCacheDir() and the fetch() isn't called earlier.

Thanks,
Ben
Back to top
View user's profile Send private message Visit poster's website
U.Tews
Administrator


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

PostPosted: Thu Jan 10, 2013 10:56 pm    Post subject: Reply with quote

Strange. I can not reproduce your problem. Is $smarty->template_objects emtpy before you call $smarty->fetch() ?
Back to top
View user's profile Send private message
bluejester
Smarty Regular


Joined: 26 Apr 2012
Posts: 55

PostPosted: Fri Jan 11, 2013 2:17 pm    Post subject: Reply with quote

Template_objects wasn't empty when I call the fetch() method.

It now works with the following (example) code :

Code:

$smarty->setCaching(Smarty::CACHING_LIFETIME_CURRENT);
$smarty->setCacheLifeTime(1800);
$smarty->setCacheId('some_cache_id');

$smarty->setCacheDir('/var/www/somedir');
echo $smarty->getCacheDir();



$today = date('Y-m-d');
$user = 'bluejester';

#New line added
unset($smarty->template_objects);
         

$smarty->setCacheDir($smarty->getCacheDir() . $user . DS . $today);
echo $smarty->getCacheDir(); // correctly outputs /var/www/somedir/bluejester/2013-01-10/

$smarty->fetch('SomeTPL.tpl', $smarty->getCacheId()); // correctly caches into /var/www/somedir/bluejester/2013-01-10/


Thanks !
Ben
Back to top
View user's profile Send private message Visit poster's website
bluejester
Smarty Regular


Joined: 26 Apr 2012
Posts: 55

PostPosted: Fri Jan 11, 2013 2:50 pm    Post subject: Reply with quote

Hello again,

I figured out why it didn't work : in my (real) code, I was querying the IsCached() method before calling SetCacheDir() again and Fetch(), that was filling template_objects before fetching...

Bug reproduction here (unzip and run test.php)

It's working now ; thanks for you help with template_objects, I wouldn't have figured this out by myself Smile

Regards,
Ben
Back to top
View user's profile Send private message Visit poster's website
U.Tews
Administrator


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

PostPosted: Fri Jan 11, 2013 3:51 pm    Post subject: Reply with quote

yep this is what I thought. The template must be cached on isCached() to avoid race condition and for performancel
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Bugs 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