php 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的內容如下: 2) 編寫程序及模板 示例程序:testCacheLess.php 所用的模板:cache.tpl 已經緩存的:{$smarty.now} {cacheless} 沒有緩存的:{$smarty.now} {/cacheless} 關于模板中部分不被緩存的解決辦法: smarty提供了強大的緩存功能。但有時我們并不希望整篇文檔都被緩存,而是有選擇的緩存某一部分內容或某一部分內容不被緩存。例如你在頁面上端使用一個帶有廣告條位置的模板,廣告條可以包含任何HTML、圖象、FLASH等混合信息. 因此這里不能使用一個靜態的鏈接,同時我們也不希望該廣告條被緩存. 這就需要在 insert 函數指定,同時需要一個函數取廣告條的內容信息。smarty也提供了這種緩存控制能力。 我們可以使用{insert}使模板的一部分不被緩存 可以使用$smarty->register_function($params,&$smarty)阻止插件從緩存中輸出, 還可以使用$smarty->register_block($params,&$smarty)使整篇頁面中的某一塊不被緩存。 下面我們真對一個簡單需求,分別說明這三種控制緩存輸出的方法。 需求:被緩存的文檔中當前時間不被緩存,隨每次刷新而變化。 1、使用insert函數使模板的一部分不被緩存 index.tpl: {insert name="get_current_time"} index.php function insert_get_current_time(){ return date("Y-m-d H:m:s"); } $smarty=new smarty(); $smarty->caching = true; if(!$smarty->is_cached()){ ....... } $smarty->display(/'index.tpl/'); 注解: 定義一個函數,函數名格式為:inser_name(array $params, object &$smarty), 函數參數可選的,如果在模板的insert方法中需要加入其他屬性,就會作為數組傳遞給用戶定義的函數。 如:{insert name=/'get_current_time/' local=/'zh/'} 在get_current_time函數中我們就可以通過$params[/'local/']來獲得屬性值。 如果在get_current_time函數中需要用到當前smarty對象的方法或屬性,就可以通過第二個參數獲得。 這時你會發現index.tpl已被緩存,但當前時間卻隨每次刷新在不斷變化。 2、使用register_function阻止插件從緩存中輸出 index.tpl: {current_time}{/div} index.php: function smarty_function_current_time($params, &$smarty){ return date("Y-m-d H:m:s"); } $smarty=new smarty(); $smarty->caching = true; $smarty->register_function(/'current_time/',/'smarty_function_current_time/',false); if(!$smarty->is_cached()){ ....... } $smarty->display(/'index.tpl/'); 注解: 定義一個函數,函數名格式為:smarty_type_name($params, &$smarty) type為function name為用戶自定義標簽名稱,在這里是{current_time} 兩個參數是必須的,即使在函數中沒有使用也要寫上。兩個參數的功能同上。
總結
以上是生活随笔為你收集整理的php smarty关闭缓存,php+Smarty的缓存操作的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 甩安琪儿一套
- 下一篇: php codeigniter 语言,p