Smarty缓存无效?

3
我在项目中使用Smarty,当我启用缓存时似乎不起作用。我使用以下结构:

index.php — 显示(index.tpl)

index.tpl — {include file=$page_center}

?module=product — $smarty->assign(”page_center” , “product.tpl”) ;

在product.php中,模板product.tpl必须加载到index.tpl的中央。当我启用缓存时,它仍然显示默认内容而不是product.tpl。当禁用缓存时,它正常工作。启用缓存时出了什么问题?
3个回答

8
您需要为每个页面使用唯一的缓存ID,以确保其正常工作:
$cacheID = 'some_unique_value_for_this_page';
$smarty->assign('page_center', 'product.tpl');
$smarty->display('index.tpl', $cacheID);

根据您在问题中提供的示例,使用查询字符串中的模块名称作为缓存ID的基础可能是有意义的。

在Smarty手册中有更多信息:http://www.smarty.net/manual/en/api.display.php


好的。我测试了一下,但不幸的是没有帮到我,因为每次一个模块调用时,缓存目录中就会创建一个新的缓存文件,这与使用 $smarty->force_compile 是一样的。我认为这种方式不是正确的缓存方式。 - mehdi
虽然这是一个老话题,但使用与该页面唯一相关的值将为您完成工作。例如,如果您的产品有一个产品ID号码,则将其用作唯一的cacheID值;Smarty将编译并缓存该特定产品页面的模板一次,然后在将来任何时候引用它时都会引用缓存版本。 - Rohaq
我的语言缓存解决方案($cache_id = $lng_id)。谢谢! - Joshua - Pendo

0

你需要创建一个动态模块!

function smarty_block_dynamic($param, $content, &$smarty) {
    return $content;
}

那么

$smarty = new Smarty
$smarty->register_block('dynamic',
                        'smarty_block_dynamic',
                        false /* this block wont be cached */);

以及你的tpl文件

Hello {$name}

this is your las update

{/dyamic}
{include file="some/dynamic/thing.tpl"}
{/dynamic}

0
在尝试解决Smarty缓存问题之前,也就是如果没有进行缓存,请检查您的templates_c文件夹是否具有读写权限。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接