I have introduced the installation of pear before. Today I will introduce one of several well-known packages of pear. If there is anything unclear, please search "PEAR tutorial" on the site to get the previous tutorial!
What we are going to introduce today is PEAR’s Cache_Lite package. Web developers’ eyes widen when they talk about speeding up, just like men’s eyes widen when they see a stunning beauty. Therefore, the first thing I want to introduce here is PEAR's Cache_lite package, you can use this package to cache any part of the web page according to your needs, thus greatly improving the page generation and loading speed!
First, go to Pear's List Packages to download the Cache_Lite installation package. When writing this tutorial, the stable version is 1.7.2, so we download this version, unzip it after downloading, and then place the file in the root directory of PEAR ( How to organize the file directory will be introduced later), and then we go to the corresponding chapter of the manual to see how to use it. You can take a look at the introduction about packages first. There is no foreplay here and we will go directly to the topic. The following is an example of Cache_Lite::get(). Let's start with this example. I added Chinese comments to the example.
require_once "Cache/Lite.php";//This is the address relative to PEAR. After finding the Lite.php file in the package you just downloaded, you should know how to deploy this folder!
$options=array(
'cacheDir'=>'/tmp/',//This is the path of the Cache, it is best to use an absolute path, which will be explained in our example
'lifeTime'=>7200, //Cache expiration time in seconds
'pearErrorMode'=>CACHE_LITE_ERROR_DIE//Error reporting mode
);
$cache=newCache_Lite($options);//Create a cache after setting the parameters
if($data=$cache->get('id_of_the_page')){//If the cache with id=id_of_the_page exists, directly echo the cached data out
// Cache hit!
// Content is in $data
// (...)
}else{//If the cache does not exist, generate a cache
// No valid cache found (you have to make and save the page)
// (...)
}
After reading the above example, do you find it is very simple? In fact, the key to caching is not how to generate and delete caches, but how to balance the relationship between static and dynamic caches, and how to rebuild the cache at the appropriate time. Let me start with an example to let everyone realize the benefits of caching! , please create a file cache under tutor (the root directory corresponding to the example in our tutorial). For *nix operating systems, please set the property to 0777, then create cache.php under the tutor folder, enter the following code
<?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 go to tutor (the root directory corresponding to the examples in our tutorial)
//Create file cache,
//*nix operating system, please set the attribute to 0777
'lifeTime'=>10,//10 seconds expiration time
'pearErrorMode'=>CACHE_LITE_ERROR_DIE
);
$cache=newCache_Lite($options);
$cache_id='cache'; //id must be unique
//Otherwise, it will conflict with other people's cache.
if($data=$cache->get($cache_id)){
//data is the result of getting data, if the cache exists and has not expired,
//Get data directly
echo$data;
}else{
//Otherwise we create a cache
// Below we deliberately use loops to consume time
while($i<10000000)
$i++;
$data='Cache creation time:'.date("Ymd H:i:s");
$data.="<p>The execution time without cache is: ".(get_microtime()-$s)."seconds";
echo$data;
$cache->save($data);
}
echo"<p>The current page takes: ".(get_microtime()-$s)."seconds</p>";
?>
Run http://127.0.0.1/tutor/cache.php in the browser, and then see if a file is generated in the cache directory. It feels very fulfilling, right? !
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 that the require files here are 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, create a cache. If it exists, the program will automatically output the cache.
?>
You can do whatever you want here,
Including executing php
Isn’t it very convenient that all database queries, including database queries, can be completed here as long as PHP allows it?
<?php
$cache->end();//Don’t forget this,
//Otherwise the cache will never be successfully created.
//This function is coming out of the output buffer
}
?>
Go and see if there is another file in the cache directory?
This concludes the introduction to cache. Here are a few things to note:
1. The ID of the cache must be unique, and some parameters can be integrated
2. It is best to write an absolute path for the cache path of the cache.
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, continue The cache operation should not be difficult.