View previous topic :: View next topic |
Author |
Message |
Jerc Smarty Rookie
Joined: 26 Jan 2010 Posts: 30 Location: Slovenia / Ljubljana
|
Posted: Fri Feb 04, 2011 6:12 am Post subject: cache only included file |
|
|
I have a question how to solve this problem.
I don't want to be index.tpl cached. But i want the included file (news_box in index.tpl) to be cached for 10 minutes. The code below, creates / rebuilds cache every 10 minutes. But the function isCached returns me false each time. Can you help me, how to solve this problem.
Code: | $cache_id_news = md5('news_'.$MC_language);
$smarty->assign('cache_id_news', $cache_id_news);
if (!$smarty->isCached('news_box.tpl', $cache_id_news)) {
....
}
$smarty->display('index.tpl'); |
index.tpl:
Code: | <div>
{include 'news_box.tpl' caching cache_id=$cache_id_news cache_lifetime=600}
</div>
|
Thanks,
Jernej |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Fri Feb 04, 2011 10:33 am Post subject: |
|
|
Your script does not know that you have cached a subtemplate and which lifetime was used. You must change your code as follow:
Code: | $cache_id_news = md5('news_'.$MC_language);
$smarty->assign('cache_id_news', $cache_id_news);
$smarty->setCacheLifetime(600);
$smarty->setCaching(1);
if (!$smarty->isCached('news_box.tpl', $cache_id_news)) {
....
}
$smarty->setCaching(0);
$smarty->display('index.tpl'); |
|
|
Back to top |
|
Jerc Smarty Rookie
Joined: 26 Jan 2010 Posts: 30 Location: Slovenia / Ljubljana
|
Posted: Fri Feb 04, 2011 11:05 am Post subject: |
|
|
Many thanks for the solution.
I tought that setCaching is working only on display, So i did not use it . |
|
Back to top |
|
Jerc Smarty Rookie
Joined: 26 Jan 2010 Posts: 30 Location: Slovenia / Ljubljana
|
Posted: Mon Feb 14, 2011 8:50 am Post subject: |
|
|
Hm.. i've noticed a problem and i don't know how to solve it.
For example:
when $smarty->isCached is called - at this moment cache is valid.
when {include..} is called, cache is not valid and more. So smarty will rebulid it. Because i've skipped part of code where it sets data, the smarty will save empty file.
Is there option to re-test isCached in the template file. So my soulution would be something like this:
Code: | <div>
{if isCached('news_box.tpl')}
{include 'news_box.tpl' caching cache_id=$cache_id_news cache_lifetime=600}
{elseif $newsData|@count > 0}
{include 'news_box.tpl' caching cache_id=$cache_id_news cache_lifetime=600}
{else}
{include 'news_box.tpl'} {* nothing to display *}
{/if} |
Or maybe something like this:
Code: | {include 'news_box.tpl' caching cache_id=$cache_id_news cache_lifetime=600 dataSet=$newsData}
|
if isset $newsData, then create cache, else do not create cache.
So in both ways smarty would save cache, only if the newsData is set and not empty. |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Mon Feb 14, 2011 2:08 pm Post subject: |
|
|
The status of the $smarty->isCached(..) call is internally saved and should be used to consider if the cache file needs to be rebuild at the {include } call.
But this does work only if inside the template same life_time and cache_id is used as on the isCached() call in the script.
I will very that this it does work as it should and let you know ASAP. |
|
Back to top |
|
Jerc Smarty Rookie
Joined: 26 Jan 2010 Posts: 30 Location: Slovenia / Ljubljana
|
Posted: Thu Feb 17, 2011 7:44 am Post subject: |
|
|
So for the test, i've put sleep for 10 seconds.
Example:
1. {include .. cache_lifetime=30}
2. open page: news_box.tpl content shows as it should
3. put sleep for 10 seconds
4. after 25 seconds i've opened page ($smarty->isCached returns true)
5. page opens without news_box.tpl content. So this happens each time when isCached is called (the tpl has a valid cache) and when tpl is called cache is not valid any more.
Code: | $smarty->setCacheLifetime(30);
$smarty->setCaching(1);
if (!$smarty->isCached('news_box.tpl', $cache_id_news)) {
....
} else {
sleep(10);
}
$smarty->setCaching(0);
{include 'news_box.tpl' caching cache_id=$cache_id_news cache_lifetime=30} |
If i put into the setCacheLifetime(20); (less then in tpl file). Would be this a good solution?
This happened few times last week, when we had high load on server. |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Thu Feb 17, 2011 11:20 am Post subject: |
|
|
In
Code: | if (!$smarty->isCached('news_box.tpl', $cache_id_news)) { |
you are using the PHP variable $cache_id_news.
In
Code: | {include 'news_box.tpl' caching cache_id=$cache_id_news cache_lifetime=30} |
$cache_id_news is a Smarty variable. Was this assigned in your script?
Code: | $smarty->assign('cache_id_news',$cache_id_news); |
If not your isCache() and {include} will work on different cache files. |
|
Back to top |
|
Jerc Smarty Rookie
Joined: 26 Jan 2010 Posts: 30 Location: Slovenia / Ljubljana
|
Posted: Thu Feb 17, 2011 1:48 pm Post subject: |
|
|
cache_id_news is always available (in php and tpl).
Here is my code (php file):
Code: | $MC_language = 'si'; // set in parent file
$cache_id_news = md5('news_'.$MC_language);
$smarty->assign('cache_id_news', $cache_id_news);
$smarty->setCacheLifetime(600);
$smarty->setCaching(1);
if (!$smarty->isCached('news_box.tpl', $cache_id_news)) {
...
$smarty->assign('newsData', $data);
}
$smarty->setCaching(0);
... // rest of the code
$smarty->display('index.tpl');
|
index.tpl:
Code: | {include 'news_box.tpl' caching cache_id=$cache_id_news cache_lifetime=600}
|
news_box.tpl:
Code: | {strip}
{if isset($newsData) && $newsData|@count > 0}
{foreach from=$newsData....
{/if}
{/strip} |
|
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Thu Feb 17, 2011 7:10 pm Post subject: |
|
|
I think I found a possible race condition. I will look further into it ASAP. |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Fri Feb 18, 2011 5:35 pm Post subject: |
|
|
A fix for this problem is now in the SVN trunk.
NOTE: You must delete existing cached and compiled template files after this update. |
|
Back to top |
|
Jerc Smarty Rookie
Joined: 26 Jan 2010 Posts: 30 Location: Slovenia / Ljubljana
|
Posted: Mon Feb 21, 2011 6:57 am Post subject: |
|
|
I think that problem has been solved.
Many thanks! |
|
Back to top |
|
|