Home > Article > Backend Development > PHP file caching smarty template application example analysis, smarty application example_PHP tutorial
This article analyzes the PHP file caching smarty template application example. Share it with everyone for your reference, the details are as follows:
1. Use cache
To enable smarty’s cache, just set caching to true and specify cache_dir.
Use cache_lefetime to specify the cache lifetime in 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 to cache different $_GETs differently
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 a custom function to handle cache
Such as:
$smarty->cache_handler_func = "myCache"; function myCache($action, &$smarty_obj, &$cache_content, $tpl_file=null, $cache_id=null, $compile_id=null){ }
This function generally determines the current operation of the cache based on $action:
switch($action){ case "read"://读取缓存内容 case "write"://写入缓存 case "clear"://清空 }
Generally use md5 ($tpl_file.$cache_id.$compile_id) as the only cache_id
If necessary, use gzcompress and gzuncompress to compress and decompress
4. Partially close the cache
To invalidate cache in certain areas (only the required cache), there are several ways:
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, then 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:
Define a block:
smarty_block_name($params,$content, &$smarty){return $content;} //name表示区域名
Registration block:
$smarty->register_block('name', 'smarty_block_name', false); //第三参数false表示该区域不被缓存
Template writing:
{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) Write 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}
Readers who are interested in more PHP-related content can check out the special topics of this site: "Basic Tutorial for Getting Started with Smarty Templates", "Summary of PHP Date and Time Usage", "Introduction Tutorial on PHP Object-Oriented Programming", "php String (string) usage summary", "php mysql database operation introductory tutorial" and "php common database operation skills summary"
I hope this article will be helpful to everyone in PHP programming.