search

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
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
Moondrop releases Block true wireless earbuds with low-latency game modeMoondrop releases Block true wireless earbuds with low-latency game modeAug 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

入职后,我才明白什么叫Cache入职后,我才明白什么叫CacheJul 31, 2023 pm 04:03 PM

事情其实是这样的,当时领导交给我一个perf硬件性能监视的任务,在使用perf的过程中,输入命令perf list,我看到了以下信息:我的任务就要让这些cache事件能够正常计数,但关键是,我根本不知道这些misses、loads是什么意思。

c语言中null和NULL的区别是什么c语言中null和NULL的区别是什么Sep 22, 2023 am 11:48 AM

c语言中null和NULL的区别是:null是C语言中的一个宏定义,通常用来表示一个空指针,可以用于初始化指针变量,或者在条件语句中判断指针是否为空;NULL是C语言中的一个预定义常量,通常用来表示一个空值,用于表示一个空的指针、空的指针数组或者空的结构体指针。

使用cache可以提高计算机运行速度这是因为什么使用cache可以提高计算机运行速度这是因为什么Dec 09, 2020 am 11:28 AM

使用cache可以提高计算机运行速度这是因为Cache缩短了CPU的等待时间。Cache是位于CPU和主存储器DRAM之间,规模较小,但速度很高的存储器。Cache的功能是提高CPU数据输入输出的速率;Cache容量小但速度快,内存速度较低但容量大,通过优化调度算法,系统的性能会大大改善。

cache是什么存储器?cache是什么存储器?Nov 25, 2022 am 11:48 AM

cache叫做高速缓冲存储器,是介于中央处理器和主存储器之间的高速小容量存储器,一般由高速SRAM构成;这种局部存储器是面向CPU的,引入它是为减小或消除CPU与内存之间的速度差异对系统性能带来的影响。Cache容量小但速度快,内存速度较低但容量大,通过优化调度算法,系统的性能会大大改善。

undefined和null是什么意思undefined和null是什么意思Nov 20, 2023 pm 02:39 PM

在JavaScript 中,undefined和null都代表着“无”的概念:1、undefined 表示一个未初始化的变量或一个不存在的属性,当声明了一个变量但没有对其赋值时,这个变量的值就是undefined,访问对象中不存在的属性时,返回的值也是undefined;2、null表示一个空的对象引用,在某些情况下,可以将对象的引用设置为null,以便释放其占用的内存。

null和undefined有什么不同null和undefined有什么不同Nov 08, 2023 pm 04:43 PM

null和undefined的区别在:1、语义含义;2、使用场景;3、与其它值的比较;4、与全局变量的关系;5、与函数参数的关系;6、可空性检查;7、性能考虑;8、在JSON序列化中的表现;9、与类型的关系。详细介绍:1、语义含义,null通常表示知道这个变量不会拥有任何有效的对象值,而undefined则通常表示变量未被赋值,或者对象没有此属性;2、使用场景等等。

什么时候用null和undefined什么时候用null和undefinedNov 13, 2023 pm 02:11 PM

null和undefined都表示缺少值或未定义的状态,根据使用场景的不同,选择使用null还是undefined有以下一些指导原则:1、当需要明确指示一个变量为空或无效时,可以使用null;2、当一个变量已经声明但尚未赋值时,会被默认设置为undefined;3、当需要检查一个变量是否为空或未定义时,使用严格相等运算符“===”来判断变量是否为null或undefined。

See all articles

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 agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.