1. Use cache
To enable smarty's cache, just set cache to true and specify cache_dir.
Use cache_lefetime to specify Cache survival time, unit is seconds
To generate multiple different caches for the same page, add the second parameter cache_id to display or fetch, such as
$smarty->display('index.tpl',$my_cache_id);
This feature can be used Perform different caches on different $_GET
2. Clear cache
clear_all_cache();//清除所有缓存 clear_cache('index.tpl');//清除index.tpl的缓存 clear_cache('index.tpl',cache_id);//清除指定id的缓存
3. Use custom caching method
Set cache_handler_func to use custom FunctionHandling cache
For example:
$smarty->cache_handler_func = "myCache"; function myCache($action, &$smarty_obj, &$cache_content, $tpl_file=null, $cache_id=null, $compile_id=null){ }
This function is generally based on $action to determine the current operation of the cache:
switch($action){ case "read"://读取缓存内容 case "write"://写入缓存 case "clear"://清空 }
Generally usedmd5($tpl_file.$cache_id.$compile_id) as the only cache_id
If necessary, you can use gzcompress and gzuncompress to compress and decompress
4. Partially close the cache
To invalidate the cache in certain areas (only the required cache), there are several methods:
insert:
Define a processing function to be used by the insert tag. The function name format is: insert_xx (array $params, object &$smarty) where xx is the name of insert, that is to say, if the function you define is insert_abc, the method used in the template is {insert name='abc'}
Parameters are passed in through $params
It can also be made into an insert plug-in. The file name is: insert.xx.PHP, the function is named: smarty_insert_aa($params,&$smarty), and the definition of xx is the same as above
register_block:
定义一个block:smarty_block_name($params,$content, &$smarty){return $content;} //name表示区域名 注册block:$smarty->register_block('name', 'smarty_block_name', false); //第三参数false表示该区域不被缓存 模板写法:{name}内容{/name}
Written as block plug-in:
1) Define a plug-in function: block.cacheless.php and place it in smarty’s plugins directory
The content of block.cacheless.php is as follows :
<?php function smarty_block_cacheless($param, $content, &$smarty) { return $content; } ?>
2) Writing programs and templates
Sample program: testCacheLess.php
<?php include('Smarty.class.php'); $smarty = new Smarty; $smarty->caching=true; $smarty->cache_lifetime = 6; $smarty->display('cache.tpl'); ?>
Template used: cache.tpl
已经缓存的:{$smarty.now}<br>{cacheless} 没有缓存的:{$smarty.now}{/cacheless}
The above is the detailed content of Detailed explanation of the application of PHP file caching smarty template. For more information, please follow other related articles on the PHP Chinese website!