Home > Backend Development > PHP Tutorial > PEAR Tutorial (2)--Introduction to Pear's Cache_PHP Tutorial

PEAR Tutorial (2)--Introduction to Pear's Cache_PHP Tutorial

WBOY
Release: 2016-07-13 17:36:49
Original
1139 people have browsed it

前面介绍了pear的安装,今天开始介绍pear的几个知名的package之一,如果有不清楚的地方请站内搜索“PEAR教程”获取前面的教程!

今天我们要介绍的是PEAR的Cache_Lite包,做web的说到提速眼睛瞪大的程度绝不亚于男人见到绝色美女时眼睛所瞪大的程度,因此,我这里第一个要介绍的就是PEAR的Cache_lite包,利用这个package可以根据你的需要缓存网页的任何一个部分,从而大大的提高了页面的生成和载入速度!

首先去Pear的List Packages下载Cache_Lite这个安装包,写这个教程的时候,稳定的版本是1.7.2,所以我们就下载这个版本,下载下来后解压缩,然后将文件放置在PEAR的根目录下(文件目录如何组织,后面有介绍),然后我们去manual对应的章节看看如何使用。那些关于包的介绍你可以先看看,我们这里没有前戏,直接切入主题,下面是Cache_Lite::get()的例子。我们就从这个例子入手,我在例子中增加了中文的注释


require_once"Cache/Lite.php";//这里是相对PEAR的地址,找到刚才下载的包中Lite.php这个文件后你就应该知道如何部署这个文件夹了!
$options=array(
cacheDir=>/tmp/,//这里是Cache的路径,最好用绝对路径,我们的例子中将会有说明
lifeTime=>7200,//缓存的失效时间,秒为单位
pearErrorMode=>CACHE_LITE_ERROR_DIE//报错模式
);
$cache=newCache_Lite($options);//参数设置完之后建立缓存
if($data=$cache->get(id_of_the_page)){//如果id=id_of_the_page这个缓存存在的话,则直接将缓存数据echo出来
// Cache hit !
// Content is in $data
// (...)
}else{//缓存不存在,则生成缓存
// No valid cache found (you have to make and save the page)
// (...)
}

看了上面的这个例子,是不是发现很简单?其实缓存的关键之处不在于如何生成和删除缓存,而在于如何平衡缓存静止和动态的关系,如何在适当的时候重建缓存。下面我开始举例,让大家体会到缓存的好处!,请在tutor(我们教程中的例子对应的根目录)下建立文件cache,*nix操作系统请设置属性为0777,然后在tutor文件夹下建立cache.php,输入下面的代码

< ?php
require_once"config.php";
require_once"Cache/Lite.php";
//The following code is to calculate the page execution time and has nothing to do with cache
functionget_microtime ()
{
list($usec,$sec)=explode( ,microtime());
return((float)$usec+(float)$sec);
}
$s= get_microtime();
//The following are the cache settings
$options=array(
cacheDir=> WEB_DIR."/cache/",
//Please check with the tutor (us The root directory corresponding to the example in the tutorial)
//Create a file cache,
//*Nix operating system, please set the property to 0777
lifeTime=>10, //10 seconds expiration time
pearErrorMode=> CACHE_LITE_ERROR_DIE
);
$cache=newCache_Lite($options);
$cache_id=cache;//The id must be unique
//Otherwise it will conflict with other people’s caches
if($data=$cache->get($cache_id)){
//data is the result of obtaining data. If the cache exists and has not expired,
//retrieve the data directly
echo$data;

}else{
// Otherwise we create a cache

// Below we deliberately use a loop to take time
while($i<10000000)
                                                                           get_microtime()-$s)."seconds";
echo$data;
$cache->save($data);

}
echo"

current Page time: ".(get_microtime()-$s)."seconds

";
?>
Run http://127.0.0.1/tutor/cache in the browser. php, and then see if a file is generated in the cache directory. Is it a sense of accomplishment? !
On the web page, we found that the first run time took about 1 second, and after the cache was generated, the time was only 1/1000 of the original. I don’t need to describe this efficiency anymore! ! In fact, the general process of the above example is: 1. Establish cache parameters, including the cache ID; 2. Check whether the cache exists based on the parameters and ID. If it exists, the cache data is obtained in the $data variable, and then echoed out, otherwise Regenerate the cache, save the results of the page in a variable, and then write the variable's data to the cache. However, this method is very inconvenient, because we must write all the output into a variable, which is actually more troublesome. It requires a lot of string connections and the code is difficult to maintain. Of course, a simple cache is best recommended. Take this approach. But don't worry, the powerful PEAR is not so mentally retarded, so it also provides another way, which is actually to get the buffer and take out the variables. Let's take a look at this simple example. The corresponding manual chapter is here.

< ?php

require_once"config.php";


require_once"Cache/Lite/Output.php";
//Note, the require file here is different
$options=array(
cacheDir=> WEB_DIR."/cache/",
lifeTime=>10,//10 seconds expiration time
pearErrorMode=> CACHE_LITE_ERROR_DIE
);
$cache=newCache_Lite_Output($options);
$cache_id=obcache;
if(!($cache->start($cache_id))){
//If it does not exist, then Create a cache. If it exists, the program will automatically output the cache
?>

You can do whatever you want here,
including executing php
including database query
as long as it is php Everything allowed can be done here
Isn’t it very convenient

< ?php
$cache->end();//Don’t forget this,
/ /Otherwise, the cache will be unsuccessful forever,
// This function is output buffer
}
? & Gt;

Go and see if there is another file in the cache directory?

That’s it for the introduction of cache. Here are a few things to pay attention to:

1. The ID of the cache must be unique, and some parameters can be integrated
2. The cache path of the cache is best written as an absolute path.
3. The focus of this section is on the comments, please read the comments carefully

In addition, through my introduction, you can read the corresponding manual on how to delete the cache. The above example is just to inspire others. If you can use the above example, the next cache operation should not be difficult.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/486625.htmlTechArticleI introduced the installation of pear before. Today I will introduce one of several well-known packages of pear. If you are not sure Please search "PEAR tutorial" on the site to get the previous tutorial! Today we...

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template