View previous topic :: View next topic |
Author |
Message |
bluejester Smarty Regular
Joined: 26 Apr 2012 Posts: 55
|
Posted: Thu Jan 10, 2013 4:35 pm Post subject: [RESOLVED] Issue with SetCacheDir() |
|
|
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 |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Thu Jan 10, 2013 6:27 pm Post subject: |
|
|
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 |
|
bluejester Smarty Regular
Joined: 26 Apr 2012 Posts: 55
|
Posted: Thu Jan 10, 2013 8:12 pm Post subject: |
|
|
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 |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Thu Jan 10, 2013 10:56 pm Post subject: |
|
|
Strange. I can not reproduce your problem. Is $smarty->template_objects emtpy before you call $smarty->fetch() ? |
|
Back to top |
|
bluejester Smarty Regular
Joined: 26 Apr 2012 Posts: 55
|
Posted: Fri Jan 11, 2013 2:17 pm Post subject: |
|
|
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 |
|
bluejester Smarty Regular
Joined: 26 Apr 2012 Posts: 55
|
Posted: Fri Jan 11, 2013 2:50 pm Post subject: |
|
|
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
Regards,
Ben |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Fri Jan 11, 2013 3:51 pm Post subject: |
|
|
yep this is what I thought. The template must be cached on isCached() to avoid race condition and for performancel |
|
Back to top |
|
|