自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

PHP模版引擎之Smarty的緩存操作技巧

開發(fā) 后端
本文介紹的是PHP引擎模版Smarty的緩存的一些操作技巧。希望對你有幫助,一起來看吧!

我們知道PHP語言作為開源社區(qū)的一員,提供了各種模板引擎,如FastTemplate,Smarty,SimpleTemplate等,而Smarty是現(xiàn)在使用得比較多的PHP模板引擎,下面介紹Smarty的緩存操作技巧。

一、使用緩存

要開啟smarty的緩存,只需將caching設為true,并指定cache_dir即可.

使用cache_lefetime指定緩存生存時間,單位為秒

要對相同頁面生成多個不同的緩存,在display或fetch中加入第二參數(shù)cache_id,如$smarty->display('index.tpl',$my_cache_id);此特性可用于對不同的$_GET進行不同的緩存

二、清除緩存

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

三、使用自定義緩存方式

設置cache_handler_func使用自定義的函數(shù)處理緩存
如:

  1. $smarty->cache_handler_func = "myCache";  
  2. function myCache($action, &$smarty_obj, &$cache_content$tpl_file=null, 
  3. $cache_id=null, $compile_id=null)  
  4. {  

該函數(shù)的一般是根椐$action來判斷緩存當前操作:

  1. switch($action){  
  2. case "read"://讀取緩存內容  
  3. case "write"://寫入緩存  
  4. case "clear"://清空  

一般使用md5($tpl_file.$cache_id.$compile_id)作為唯一的cache_id

如果需要,可使用gzcompress和gzuncompress來壓縮和解壓

四、局部關閉緩存

要在某些區(qū)域使緩存失效(只對需要的緩存),有幾種方法:

insert:

定義一個insert標簽要使用的處理函數(shù),函數(shù)名格式為:

  1. insert_xx(array $params, object &$smarty

其中的xx是insert的name,也就是說,如果你定義的函數(shù)為insert_abc,則模板中使用方法為

  1. {insert name='abc'

參數(shù)通過$params傳入

也可以做成insert插件,文件名命名為:insert.xx.php,函數(shù)命名為:smarty_insert_aa($params,&$smarty),xx定義同上

register_block:

定義一個

  1. block:  
  2. smarty_block_name($params,$content, &$smarty)  
  3. {  
  4. return $content;  
  5. //name表示區(qū)域名 

注冊

  1. block:$smarty->register_block('name''smarty_block_name', false);   
  2. //第三參數(shù)false表示該區(qū)域不被緩存 

模板寫法:{name}內容{/name}

寫成block插件:

1)定義一件插件函數(shù):block.cacheless.php,放在smarty的plugins目錄

block.cacheless.php的內容如下:

  1. <?php  
  2. function smarty_block_cacheless($param$content, &$smarty) {  
  3. return $content;  
  4. }  
  5. ?> 

2) 編寫程序及模板

示例程序:testCacheLess.php

  1. <?php  
  2. include('Smarty.class.php');  
  3. $smarty = new Smarty;  
  4. $smarty->caching=true;  
  5. $smarty->cache_lifetime = 6;  
  6. $smarty->display('cache.tpl');  
  7. ?> 

所用的模板:cache.tpl

已經緩存的

  1. {$smarty.now}<br>  
  2. {cacheless} 

沒有緩存的:

  1. {$smarty.now}  
  2. {/cacheless} 

到這,就給大家介紹完了。建議大家看看這兩篇文章,《PHP模板之Smarty教程》和《詳細介紹PHP模板引擎Smarty》,供大家參考。

【編輯推薦】

  1. 讓PHP網站跑的更快 如何優(yōu)化PHP
  2. 警惕 PHP程序員最易犯10種錯誤
  3. 高效PHP程序必知的53個技巧
  4. PHP愛好者請堅定你們的信念!
  5. 提高PHP速度的幾種辦法

 

責任編輯:于鐵 來源: 大家論壇
相關推薦

2011-07-07 16:15:20

Smarty

2011-07-07 13:48:35

Smarty

2011-05-19 11:03:02

PHPDwoo

2009-12-01 19:23:22

PHP緩存技術

2009-11-30 13:15:27

PHP模板Smarty

2011-05-19 10:39:12

2009-12-10 17:27:39

PHP操作Cookie

2009-12-10 15:41:35

PHP文件操作

2024-04-29 08:05:34

NacosJava數(shù)據(jù)結構

2009-03-04 10:53:39

gettextsmartyphp

2021-02-15 12:11:00

開發(fā)技巧

2009-12-10 16:35:08

PHP操作文章列表

2011-03-11 15:53:00

LAMP優(yōu)化

2011-07-15 14:01:50

PHP模板引擎

2015-09-09 10:20:00

php緩存技術

2009-11-23 17:56:44

PHP緩存機制

2009-12-02 13:53:12

PHP使用技巧

2009-12-04 10:19:11

PHP hack

2020-11-06 00:00:00

PHP技巧后門

2020-02-19 20:24:35

PHP緩存靜態(tài)
點贊
收藏

51CTO技術棧公眾號