Smarty核心内容:Caching [缓存]

来源:互联网 发布:php开源论坛 wap 编辑:程序博客网 时间:2024/05/17 17:39

Caching [缓存]
Setting Up Caching [建立缓存]

 

require('Smarty.class.php');$smarty = new Smarty;$smarty->caching = 2; // lifetime is per cache// set the cache_lifetime for index.tpl to 5 minutes$smarty->cache_lifetime = 300;$smarty->display('index.tpl');// set the cache_lifetime for home.tpl to 1 hour$smarty->cache_lifetime = 3600;$smarty->display('home.tpl');



// NOTE: the following $cache_lifetime setting will not work when $caching = 2.//提示:在$chching=2后面的$chche_lifetime不会起作用。// The cache lifetime for home.tpl has already been set// to 1 hour, and will no longer respect the value of $cache_lifetime.// home.tpl的缓存会话期设为1小时后,不会再按$cache_lifetime的值改变。// The home.tpl cache will still expire after 1 hour.// home.tpl的缓存会在1小时后结束。$smarty->cache_lifetime = 30; // 30 seconds$smarty->display('home.tpl');


Multiple Caches Per Page [每页多个缓存]

require('Smarty.class.php');$smarty = new Smarty;$smarty->caching = true;$my_cache_id = $_GET['article_id'];if(!$smarty->is_cached('index.tpl',$my_cache_id)) { // No cache available, do variable assignments here. $contents = get_database_contents(); $smarty->assign($contents);}$smarty->display('index.tpl',$my_cache_id); 


 


Cache Groups [缓存集合]

require('Smarty.class.php');$smarty = new Smarty;$smarty->caching = true;// clear all caches with "sports|basketball" as the first two cache_id groups$smarty->clear_cache(null,"sports|basketball");// clear all caches with "sports" as the first cache_id group. This would// include "sports|basketball", or "sports|(anything)|(anything)|(anything)|..."$smarty->clear_cache(null,"sports");$smarty->display('index.tpl',"sports|basketball");



Controlling Cacheability of Plugins' Output [控制插件输出的缓冲能力]


index.php:

require('Smarty.class.php');$smarty = new Smarty;$smarty->caching = true;function remaining_seconds($params, &$smarty) { $remain = $params['endtime'] - time(); if ($remain >=0) return $remain . " second(s)"; else return "done";}$smarty->register_function('remaining', 'remaining_seconds', false, array('endtime'));if (!$smarty->is_cached('index.tpl')) { // fetch $obj from db and assign... $smarty->assign_by_ref('obj', $obj);}$smarty->display('index.tpl');



index.tpl:

Time Remaining: {remain endtime=$obj->endtime}


或:

 

index.php:

require('Smarty.class.php');$smarty = new Smarty;$smarty->caching = true;function smarty_block_dynamic($param, $content, &$smarty) { return $content;}$smarty->register_block('dynamic', 'smarty_block_dynamic', false);$smarty->display('index.tpl');



index.tpl:

Page created: {"0"|date_format:"%D %H:%M:%S"}

{dynamic}

Now is: {"0"|date_format:"%D %H:%M:%S"}

... do other stuff ...

{/dynamic}
 

原创粉丝点击