php专区

 首页 > php专区 > PHP高级 > 缓存异常处理 > PHP之三大缓存:smarty缓存、客户端缓存、PDO缓存之smarty缓存

PHP之三大缓存:smarty缓存、客户端缓存、PDO缓存之smarty缓存

分享到:
【字体:

smarty缓存

 

a.基本smarty缓存

1)开启smarty缓存机制 smarty对象的$caching属性true或者1代表开启缓存,false或者0代表关闭缓存
$smarty->caching=true

2)设置缓存时间 smarty对象的cache_lifetime属性值为整型单位为秒
$smarty->cache_lifetime【单位为秒】

3)设置smarty编译前检查缓存是否需要更新 smarty对象的$compile_check属性true代表检查false代表不检查
$smarty->compile_check=true
注意:为了最大性能,确定将$compile_check设为”false”.注意:如果设为了”false”,虽然模板文件被修改,但你不会看到修改结果,因为模板没有得到重新编译

4)判断是否已经缓存了模板 samrty对象的boolean is_cached (string $template, [string $cache_id])方法
$smarty->is_cached(‘index.tpl’[,$cache_id])
注意:在指定模板的缓存存在是返回真
$template指定检查的模板文件名
$cache_id如果模板有多个缓存的话,可以通过其指定缓存号

5)清除缓存 smarty对象的void clear_all_cache(int $expire_time)方法
$smarty->clear_all_cache(),其中$expire_time是指超过其指定的时间的所有缓存都清除

6)清除指定模板缓存 smarty对象的void clear_cache(string $template [,string $cache_id[,string $compile_id [,int$expire_time]]])
$smarty->clear_cache(‘index.tpl’)
$smarty->clear_cache(‘index.tpl’,$caceh_id)
其中:$cache_id是指如果这个模板有多个缓存,你可以通过其指定要清除缓存的缓存号
$compile_id是指如果这个模板有多个缓存,你可以通过其指定要清除缓存编译号
$expire_time用来指定超过某一时间(以秒为单位)的缓存才会被清除

7)一个页面设置多个缓存可以向smarty对象的display方法传递第二个cache_id参数
$smarty->display(‘index.tpl’,$cache_id)
注意:$cache_id指定缓存的ID,如果已经缓存直接获取缓存ID所指定的缓存内容,否则建立相应缓存ID的缓存文件
保证传递给is_cache()和clear_cache()方法的cache_id为同一个缓存ID
可以通过将clear_cache()的第一个参数设置为null,然后指定第二个参数即$cache_id来清除指定cache_id的缓存文件

8)设置缓存目录 smarty对象的cache_dir属性,值可以为相对路径【相对运行的PHP脚本】也可以是绝对路径
$smarty->cache_dir=’../cache’
注意:默认的缓存目录为’./cache’,不推荐将缓存目录放在WEB服务器根目录下

b.smarty局部缓存机制

1)使用smarty的insert函数使局部不缓存

2)使用register_function阻止插件从缓存中输出

3)使用register_block使用整个页面中的一部分不缓存
补充说明:smarty的function、block、insert插件的用法
insert的用法
index.tpl
function插件
index.tpl

 

{{insert name='getNow'}}
index.php
function insert_getNow($params,&$smarty){
	return time();
}

 

$smarty->register_function(‘getNow’,'smarty_function_getNow’,false);
register_function的第三个参数设置为false能够阻止插件从缓存中输出

block插件
index.tpl

 

{{getNow}}
{{/getNow}}
index.php
function smarty_block_getNow($params,&$smarty){
return time();
}

$smarty->register_block(‘getNow’,'smarty_block_getNow’,false);
register_block的第三个参数设置为false能够使用getNow块不缓存

 

分享到:
在Windows上安装PHP的Memcached扩展
一、安装Memcached 1、下载Memcached Win32的安装程序,下载地址 http://code.jellycan.com/memcached直接下载Win32的exe文件点击这里。 2、将下载来的zip包解压到一个目录下如(E:\iisweb\memcached)这个目录下应当是一个memcache.exe文件。 3、运行CMD命令提示符窗口,注意:你的系统是Windows7的话就需要以管理...
49点提高PHP编程效率,引入缓存机制提升性...
1.用单引号代替双引号来包含字符串,这样做会更快一些。因为会在双引号包围的字符串中搜寻变量,单引号则不会,注意:只有echo能这么做,它是一种可以把多个字符串当作参数的“”(译注:PHP手册中说echo是结构,不是真正的函数,故把函数加上了双引号)。 2.如果能将类的方法定义成static,就尽量定义成static,它的速...
  •         php迷,一个php技术的分享社区,专属您自己的技术摘抄本、收藏夹。
  • 在这里……