Home > Backend Development > PHP Tutorial > Detailed explanation of the application of PHP file caching smarty template

Detailed explanation of the application of PHP file caching smarty template

黄舟
Release: 2023-03-06 16:04:02
Original
1337 people have browsed it

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);
Copy after login

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的缓存
Copy after login

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){
}
Copy after login

This function is generally based on $action to determine the current operation of the cache:

switch($action){
case "read"://读取缓存内容
case "write"://写入缓存
case "clear"://清空
}
Copy after login

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}
Copy after login

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;
}
?>
Copy after login


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

<?php
include(&#39;Smarty.class.php&#39;);
$smarty = new Smarty;
$smarty->caching=true;
$smarty->cache_lifetime = 6;
$smarty->display(&#39;cache.tpl&#39;);
?>
Copy after login

Template used: cache.tpl

已经缓存的:{$smarty.now}<br>{cacheless}
没有缓存的:{$smarty.now}{/cacheless}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template