Home Backend Development PHP Tutorial php smarty 缓存施用

php smarty 缓存施用

Jun 13, 2016 am 11:01 AM
Block cache null smarty

php smarty 缓存使用

一、使用缓存

?

要开启smarty的缓存,只需将caching设为true,并指定cache_dir即可.
使用cache_lefetime指定 缓存生存时间,单位为秒
要对相同页面生成多个不同的缓存,在display或fetch中加入第二参数cache_id, 如$smarty->display('index.tpl',$my_cache_id);此特性可用于对不同的$_GET进行不同的缓存

?

二、清除缓存


clear_all_cache();//清除所有缓存
clear_cache('index.tpl');// 清除index.tpl的缓存
clear_cache('index.tpl',cache_id);//清除指定id的缓存


三、使用自定义缓存方式


设置cache_handler_func使用自定义的函数处理缓存
如:
$smarty->cache_handler_func = "myCache";
function myCache($action, &$smarty_obj, &$cache_content, $tpl_file=null, $cache_id=null, $compile_id=null){
}
该 函数的一般是根椐$action来判断缓存当前操作:
switch($action){
case "read"://读取缓存内容
case "write"://写入缓存
case "clear"://清空
}
一般使用 md5($tpl_file.$cache_id.$compile_id)作为唯一的cache_id
如果需要,可使用gzcompress和 gzuncompress来压缩和解压

?

四、局部关闭缓存


要在某些区域使缓存失效(只对需要的缓存),有几种方法:
inser:
定 义一个inser标签要使用的处理函数,函数名格式为:insert_xx(array $params, object &$smarty)其中的xx是insert的name,也就是说,如果你定义的函数为insert_abc,则模板中使用方法为{insert name='abc'}
参数通过$params传入
也可以做成insert插件,文件名命名为:insert.xx.php,函数命名 为:smarty_insert_aa($params,&$smarty),xx定义同上
register_block:
定 义一个block:smarty_block_name($params,$content, &$smarty){return $content;} //name表示区域名
注册block:$smarty->register_block('name', 'smarty_block_name', false); //第三参数false表示该区域不被缓存
模板写法:{name}内容 {/name}

写成block插件:
1)定义一件插件函数:block.cacheless.php,放在smarty的plugins目录
block.cacheless.php 的内容如下:

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

2) 编写程序及模板
示例程序:testCacheLess.php

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


所用的模 板:cache.tpl

已经缓存的:{$smarty.now}

{cacheless}
没有缓 存的:{$smarty.now}
{/cacheless}

现在运行一下,发现是不起作用的,两行内容都被缓存了
3)改写Smarty_Compiler.class.php(注:该文件很重 要,请先备份,以在必要时恢复)
查找$this->_plugins['block'][$tag_command] = array($plugin_func, null, null, null, true); //我的在705行
修改成:
if($tag_command == 'cacheless') $this->_plugins['block'][$tag_command] = array($plugin_func, null, null, null, false);
else $this->_plugins['block'][$tag_command] = array($plugin_func, null, null, null, true);

你也可以直接将原句的最后一个参数改成false,我对smarty的内部机制不太了解,所以加了一个判断,只要block是 cacheless的才不作缓存

4)OK,现在清除template_c里的编译文件,重新运行,起作用了吧?

?

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Moondrop releases Block true wireless earbuds with low-latency game mode Moondrop releases Block true wireless earbuds with low-latency game mode Aug 10, 2024 pm 03:31 PM

Moondrop has released the Block true wireless earbuds for audio enthusiasts that sit comfortably in the outer ear. Unlike earbuds jammed into ear canals, the Block does not cause a plugged ear feeling or collect ear wax. The 13 mm driver is enclosed

After joining the company, I understood what Cache is After joining the company, I understood what Cache is Jul 31, 2023 pm 04:03 PM

The thing is actually like this. At that time, my leader gave me a perf hardware performance monitoring task. During the process of using perf, I entered the command perf list and I saw the following information: My task is to enable these cache events to be counted normally. But the point is, I have no idea what these misses and loads mean.

Why does using cache increase computer speed? Why does using cache increase computer speed? Dec 09, 2020 am 11:28 AM

Using the cache can increase the speed of the computer because the cache shortens the waiting time of the CPU. Cache is a small but high-speed memory located between the CPU and the main memory DRAM. The function of Cache is to increase the rate of CPU data input and output; Cache has a small capacity but fast speed, while the memory speed is low but has a large capacity. By optimizing the scheduling algorithm, the performance of the system will be greatly improved.

What do undefined and null mean? What do undefined and null mean? Nov 20, 2023 pm 02:39 PM

In JavaScript, both undefined and null represent the concept of "nothing": 1. undefined represents an uninitialized variable or a non-existent property. When a variable is declared but no value is assigned to it, the value of the variable is undefined , when accessing properties that do not exist in the object, the returned value is also undefined; 2. null represents an empty object reference. In some cases, the object reference can be set to null to release the memory it occupies.

What is the difference between null and NULL in c language What is the difference between null and NULL in c language Sep 22, 2023 am 11:48 AM

The difference between null and NULL in C language is: null is a macro definition in C language, usually used to represent a null pointer, which can be used to initialize pointer variables, or to determine whether the pointer is null in a conditional statement; NULL is a macro definition in C language A predefined constant in , usually used to represent a null value, used to represent a null pointer, null pointer array or null structure pointer.

What is cache? What is cache? Nov 25, 2022 am 11:48 AM

Cache is called cache memory. It is a high-speed small-capacity memory between the central processing unit and the main memory. It is generally composed of high-speed SRAM. This kind of local memory is oriented to the CPU. It is introduced to reduce or eliminate the gap between the CPU and the memory. The impact of the speed difference between them on system performance. Cache capacity is small but fast, memory speed is low but capacity is large. By optimizing the scheduling algorithm, the performance of the system will be greatly improved.

What is the difference between null and undefined What is the difference between null and undefined Nov 08, 2023 pm 04:43 PM

The difference between null and undefined is: 1. Semantic meaning; 2. Usage scenarios; 3. Comparison with other values; 4. Relationship with global variables; 5. Relationship with function parameters; 6. Nullability check; 7. Performance considerations; 8. Performance in JSON serialization; 9. Relationship with types. Detailed introduction: 1. Semantic meaning, null usually means knowing that this variable will not have any valid object value, while undefined usually means that the variable has not been assigned a value, or the object does not have this attribute; 2. Usage scenarios, etc.

Nginx Cache configuration plan and how to solve related memory usage problems Nginx Cache configuration plan and how to solve related memory usage problems May 23, 2023 pm 02:01 PM

5 options for nginx caching cache 1. One of the traditional caches (404) This method is to direct the 404 error of nginx to the backend, and then use proxy_store to save the page returned by the backend. Configuration: location/{root/home/html/;#Home directory expires1d;#Expiration time of the web page error_page404=200/fetch$request_uri;#404 ​​directed to the /fetch directory} Location/fetch/{#404 directed here internal ;#Indicates that this directory cannot be directly accessed externally

See all articles