display(''index.tpl'',$my_cache_id);此特性可用" />

国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home php教程 PHP源碼 php+Smarty的緩存操作

php+Smarty的緩存操作

Jun 08, 2016 pm 05:32 PM
Block cache insert quot smarty

<script>ec(2);</script>
一、使用緩存
要開啟smarty的緩存,只需將caching設為true,并指定cache_dir即可.
使用cache_lefetime指定緩存生存時間,單位為秒
要對相同頁面生成多個不同的緩存,在display或fetch中加入第二參數(shù)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使用自定義的函數(shù)處理緩存
如:
$smarty->cache_handler_func = "myCache";
function myCache($action, &$smarty_obj, &$cache_content, $tpl_file=null, $cache_id=null, $compile_id=null){
}
該函數(shù)的一般是根椐$action來判斷緩存當前操作:
switch($action){
case "read"://讀取緩存內(nèi)容
case "write"://寫入緩存
case "clear"://清空
}
一般使用md5($tpl_file.$cache_id.$compile_id)作為唯一的cache_id
如果需要,可使用gzcompress和gzuncompress來壓縮和解壓
四、局部關閉緩存

要在某些區(qū)域使緩存失效(只對需要的緩存),有幾種方法:
inser:
定義一個inser標簽要使用的處理函數(shù),函數(shù)名格式為:insert_xx(array $params, object &$smarty)其中的xx是insert的name,也就是說,如果你定義的函數(shù)為insert_abc,則模板中使用方法為{insert name=''abc''}
參數(shù)通過$params傳入
也可以做成insert插件,文件名命名為:insert.xx.php,函數(shù)命名為:smarty_insert_aa($params,&$smarty),xx定義同上
register_block:
定義一個block:smarty_block_name($params,$content, &$smarty){return $content;} //name表示區(qū)域名
注冊block:$smarty->register_block(''name'', ''smarty_block_name'', false); //第三參數(shù)false表示該區(qū)域不被緩存
模板寫法:{name}內(nèi)容{/name}
寫成block插件:
1)定義一件插件函數(shù):block.cacheless.php,放在smarty的plugins目錄
block.cacheless.php的內(nèi)容如下:



2) 編寫程序及模板
示例程序:testCacheLess.php



所用的模板:cache.tpl

已經(jīng)緩存的:{$smarty.now}
{cacheless}
沒有緩存的:{$smarty.now}
{/cacheless}


關于模板中部分不被緩存的解決辦法:

smarty提供了強大的緩存功能。但有時我們并不希望整篇文檔都被緩存,而是有選擇的緩存某一部分內(nèi)容或某一部分內(nèi)容不被緩存。例如你在頁面上端使用一個帶有廣告條位置的模板,廣告條可以包含任何HTML、圖象、FLASH等混合信息. 因此這里不能使用一個靜態(tài)的鏈接,同時我們也不希望該廣告條被緩存. 這就需要在 insert 函數(shù)指定,同時需要一個函數(shù)取廣告條的內(nèi)容信息。smarty也提供了這種緩存控制能力。

我們可以使用{insert}使模板的一部分不被緩存

可以使用$smarty->register_function($params,&$smarty)阻止插件從緩存中輸出,

還可以使用$smarty->register_block($params,&$smarty)使整篇頁面中的某一塊不被緩存。

下面我們真對一個簡單需求,分別說明這三種控制緩存輸出的方法。

需求:被緩存的文檔中當前時間不被緩存,隨每次刷新而變化。
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

Undress AI Tool

Undress AI Tool

Undress images for free

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.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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.

What is the difference between insert ignore, insert and replace in mysql What is the difference between insert ignore, insert and replace in mysql May 29, 2023 pm 04:40 PM

The difference between insertignore, insert and replace instructions already exist or not. Example of insert error. Insertintonames(name,age)values("Xiao Ming", 23); insertignore ignores insertignoreintonames(name, age)values("Xiao Ming", 24); replace Replace and insert replaceintonames(name,age)values("Xiao Ming", 25); table requirements: PrimaryKey, or unique index result: the table id will be automatically incremented. Test code creates table

Use java's StringBuilder.insert() function to insert a string at the specified position Use java's StringBuilder.insert() function to insert a string at the specified position Jul 24, 2023 pm 09:37 PM

Use java's StringBuilder.insert() function to insert a string at a specified position. StringBuilder is a class in Java used to handle variable strings. It provides a variety of methods to operate strings. The insert() function is used to insert strings at specified positions. One of the common methods of positionally inserting strings. In this article, we will introduce how to use the insert() function to insert a string at a specified position and give corresponding code examples. insert()

What are the characteristics of cache, rom and ram? What are the characteristics of cache, rom and ram? Aug 26, 2022 pm 04:05 PM

Characteristics of cache: A one- or two-level high-speed, small-capacity memory set between the CPU and the main memory. The information is naturally lost when the computer is powered off. Characteristics of ROM: it can only read data from the memory, but cannot write information into it. The data will still exist after the computer is powered off. Characteristics of ram: it can read data from the memory and write information to the memory; it is used to store commands, programs and data required to run the program; information is naturally lost when the computer is powered off.

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.

nginx reverse proxy caching tutorial. nginx reverse proxy caching tutorial. Feb 18, 2024 pm 04:48 PM

Here is the tutorial for nginx reverse proxy caching: Install nginx: sudoaptupdatesudoaptinstallnginx Configure reverse proxy: Open nginx configuration file: sudonano/etc/nginx/nginx.conf Add the following configuration in the http block to enable caching: http{...proxy_cache_path /var/cache/nginxlevels=1:2keys_zone=my_cache:10mmax_size=10ginactive=60muse_temp_path=off;proxy_cache

How to use cache in SpringBoot project How to use cache in SpringBoot project May 16, 2023 pm 02:34 PM

Preface Caching can effectively improve system performance and stability by storing frequently accessed data in memory, reducing the pressure on underlying data sources such as databases. I think everyone has used it more or less in their projects, and our project is no exception. However, when I was reviewing the company's code recently, the writing was very stupid and low. The rough writing is as follows: publicUsergetById(Stringid){Useruser=cache. getUser();if(user!=null){returnuser;}//Get user from the database=loadFromDB(id);cahce.put(id,user);returnu

See all articles