Home  >  Article  >  Backend Development  >  Smarty’s cache operation skills_PHP tutorial

Smarty’s cache operation skills_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:39:07730browse

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 in display or fetch, such as $smarty->display(index.tpl,$my_cache_id); This feature can be used to perform different caches on different $_GETs. Cache

2. Clear cache

clear_all_cache();//Clear all caches
clear_cache(index.tpl);//Clear the cache of index.tpl
clear_cache(index.tpl,cache_id);//Clear the cache of the specified 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"://Read cache content
case "write"://write cache
case "clear"://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, use it in the template. The method 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 represents the area name
Register block:$smarty->register_block(name, smarty_block_name, false); //The third parameter false means that this area is not cached
Template writing: {name}content{/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:

function smarty_block_cacheless($param, $content, &$smarty) {
Return $content;
}
?>

2) Write programs and templates
Sample program: testCacheLess.php

include(Smarty.class.php);
$smarty = new Smarty;
$smarty->caching=true;
$smarty->cache_lifetime = 6;
$smarty->display(cache.tpl);
?>

Template used: cache.tpl

Cached: {$smarty.now}

{cacheless}
Not cached:{$smarty.now}
{/cacheless}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/486427.htmlTechArticle1. Using 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 times for the same page...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn