PEAR tutorial (II) -- Pear Cache introduction

Source: Internet
Author: User
PEAR tutorial (II) -- Pear's Cache introduction. read PEAR tutorial (II) -- Pear's Cache introduction. we have introduced the installation of pear, today, we will introduce one of pear's several well-known packages. if you have any questions, please search for "PEAR tutorial" on the site to get the previous tutorial! Today we will introduce the Cache_Lite package of PEAR. we will talk about how to speed up your eyes on the web. "> <

We have introduced the installation of pear. today we will introduce one of several well-known pear packages. if you have any questions, please search for the "PEAR tutorial" on the site to get the previous tutorial!

Today, we will introduce the Cache_Lite package of PEAR. when it comes to the web, the eye widening speed is no less than what men stare at when they see the stunning beauty. therefore, the first package I want to introduce here is the Cache_lite package of PEAR. this package can cache any part of the webpage as needed, greatly improving the page generation and loading speed!

First, go to the Pear List Packages to download the Cache_Lite installation package. when writing this tutorial, the stable version is 1.7.2, so we will download this version and decompress it, then place the file in the PEAR root directory (how to organize the file directory, which will be described later). then we will go to the relevant manual Chapter to see how to use it. You can take a look at the package introduction. we have no foreplays here. we will directly discuss the topic. The following is an example of Cache_Lite: get. Let's start with this example. I added a Chinese comment in this example.


Require_once "Cache/Lite. php"; // This is relative to the PEAR address. find the file Lite. php in the downloaded package and you will know how to deploy this folder!
$ Options = array (
'Cachedir' => '/tmp/', // Here is the Cache path, it is best to use an absolute path, which will be described in our example.
'Lifetime' => 7200, // cache expiration time, in seconds
'Pearerrormode' => CACHE_LITE_ERROR_DIE // error mode
);
$ Cache = newCache_Lite ($ options); // after the parameter is set, the cache is created.
If ($ data = $ cache-> get ('id _ of_the_page ') {// if the cache id = id_of_the_page exists, the cached data is directly echo
// Cache hit!
// Content is in $ data
//(...)
} Else {// if the cache does not exist, the cache is generated.
// No valid cache found (you have to make and save the page)
//(...)
}

After reading the example above, did you find it very easy? In fact, the key to caching is not how to generate and delete a cache, but how to balance the static and dynamic relationship between the cache and how to recreate the cache when appropriate. The following is an example to show you the benefits of caching !, Create a file cache in tutor (the root directory corresponding to the example in our tutorial). * Set the attribute to 0777 for the nix operating system and create a cache in the tutor folder. php: Enter the following code:

<? Php
Require_once "config. php ";
Require_once "Cache/Lite. php ";
// The following code calculates the page execution time, regardless of the cache.
Functionget_microtime ()
{
List ($ usec, $ sec) = explode ('', microtime ());
Return (float) $ usec + (float) $ sec );
}
$ S = get_microtime ();
// The cache settings are as follows:
$ Options = array (
'Cachedir' => WEB_DIR. "/cache /",
// In tutor (the root directory corresponding to the example in our tutorial)
// Create a file cache,
// * Set the nix Operating System attribute 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 others' cache
If ($ data = $ cache-> get ($ cache_id )){
// Data is the result of obtaining data. if the cache exists and does not expire,
// Retrieve data directly
Echo $ data;
 
} Else {
// Otherwise, we will create a cache
 
// Below we intentionally use cycles for time consumption
While ($ I <10000000)
$ I ++;
$ Data = 'cache creation time: '. date ("Y-m-d H: I: s ");
$ Data. ="

The execution time without caching is: ". (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. php in the browser and check whether a file is generated in the cache Directory. Is it a sense of accomplishment ?!
On the web page, we found that the first running time was about one second, and the cache generated was only 1/1000 of the original time. I do not need to describe the efficiency !! In fact, the general process of the above example is: 1. create cache parameters, including the cache ID; 2. check whether the cache exists based on the parameter and ID. If yes, the cached data is obtained in the $ data variable and then echo it out. Otherwise, the cache is regenerated, save the page result in the variable, and then write the variable data to the cache. However, this method is inconvenient, because all outputs must be written to a variable, which is actually troublesome. many string connections are required, and the code is difficult to maintain, of course, we recommend that you use this method for simple cache. But don't worry, the powerful PEAR won't be so mentally retarded, so it also provides another way, in fact, to get the buffer to retrieve the variables. let's take a look at this simple example, the corresponding manual section is here.

<? Php
Require_once "config. php ";
 
Require_once "Cache/Lite/Output. php ";
// Note That The require file is different here.
$ 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, a cache is created. if it exists, the program automatically outputs the cache.
?>
 
What do you want to do here,
Including executing php
Including database queries
All php-enabled tasks can be completed here.
Is it convenient?
 
<? Php
$ Cache-> end (); // never forget this,
// Otherwise, the cache will never be established successfully,
// This function is in output buffer.
}
?>

Check whether another file exists in the cache directory?
Cache is introduced here. There are several points to note below:
1. the cache ID must be unique and some parameters can be integrated
2. it is best to write an absolute path to the cache path.
3. this section focuses on comments. read the comments carefully.

In addition, through my introduction, you can refer to the corresponding manual for how to delete the cache. the above example is to introduce others. if the above example is used, the next cache operation should not be difficult.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.