Pear Tutorial (ii)--pear's cache introduction _php tutorial

Source: Internet
Author: User
Tags pear

The previous introduction of pear installation, today began to introduce pear one of several well-known package, if there is unclear place please stand inside search "Pear Tutorial" get the previous tutorial!

Today we are going to introduce is pear Cache_lite package, do the Web talk about speed up the eyes of the eye is no less than the extent of the man to see the beauty of the eyes of the large extent, therefore, I here the first to introduce is Pear cache_lite bag, The package can be used to cache any part of the page according to your needs, thus greatly improving the page generation and loading speed!

First go to Pear's list packages download Cache_lite This installation package, write this tutorial, The stable version is 1.7.2, so we download this version, download it, unzip it, and then put the file in the root directory of pear (how the file directory is organized, followed by the introduction), then we go to manual the corresponding chapter to see how to use. The introduction of the package you can see first, we do not have foreplay, directly into the topic, the following is the example of Cache_lite::get (). Let's start with this example, I've added a Chinese comment in the example


Require_once "cache/lite.php";//here is the relative pear address, find the package you just downloaded lite.php This file you should know how to deploy this folder!
$options =array (
cachedir=>/tmp/,//here is the path to the cache, preferably with an absolute path, our example will be explained
lifetime=>7200,//cache expiration time, in seconds
pearerrormode=>cache_lite_error_die//Error mode
);
$cache =newcache_lite ($options);//The cache is established after the parameter is set
if ($data = $cache->get (id_of_the_page)) {//If id=id_of_the_page this cache exists, echo the cached data directly
Cache hit!
Content is in $data
// (...)
}else{//cache does not exist, the cache is generated
No Valid cache found (you have to make and save the page)
// (...)
}

See the above example, is it easy to find? In fact, the key to caching is not how to generate and delete caches, but how to balance the cache static and dynamic relationships, how to rebuild the cache when appropriate. Let me start by giving you an example of the benefits of caching! , build the file under tutor (the root directory for the example in our tutorial) Cache,*nix operating system, set the property to 0777, and then create cache.php under 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, which is independent of the cache
Functionget_microtime ()
{
List ($usec, $sec) =explode (, Microtime ());
return (float) $usec + (float) $sec);
}
$s = get_microtime ();
The following are the cached settings
$options =array (
Cachedir=> Web_dir. " /cache/",
Please tutor (the root directory for the example in our tutorial)
Create a file cache under
*nix operating system, set the property to 0777
LIFETIME=>10,//10 Second Failure time
Pearerrormode=> Cache_lite_error_die
);
$cache =newcache_lite ($options);
$cache _id=cache;//id must be the only
Otherwise it's going to clash with someone else's cache.
if ($data = $cache->get ($cache _id)) {
The data is the result of getting it, and if the cache exists and is not expired,
Direct Data Extraction
Echo$data;

}else{
Otherwise we build the cache

Here we deliberately use loops to time-consuming
while ($i <10000000)
$i + +;
$data = time the cache was established:. Date ("y-m-d h:i:s");
$data. = "

No cache execution time is: ". (Get_microtime ()-$s). " Seconds ";
Echo$data;
$cache->save ($data);

}
Echo

The current page is time consuming: ". (Get_microtime ()-$s). " Seconds

";
?>
Run http://127.0.0.1/tutor/cache.php in the browser and see if a file is generated in the cache directory, is there a sense of accomplishment?!
On the Web page we found that the first run time takes about 1 seconds, after the cache is generated only for the original 1/1000, so the efficiency does not need me to describe the extra!! In fact, the general flow of the above example is, 1. Set up the cache parameters, including the cached id;2. Check that the cache exists based on the parameters and ID, and if so, the cached data is fetched in the $data variable, then echo comes out, or the cache is regenerated, and the results of the page are saved in the variable. The data for the variable is then written to the cache. However, this way is inconvenient, because all the output we have to write it into a variable, it is actually more troublesome, to use a lot of string connection, code is difficult to maintain, of course, the simple cache is best recommended to use this way. But don't worry, the mighty pear won't be so retarded, so it also provides another way to get the buffer out of the variable, and here's a simple example, corresponding to the manual chapter 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 Second Failure 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 the cache is established, and the program automatically caches the output.
?>

You can do whatever you want here.
including executing PHP
including database queries
As long as PHP is allowed, it can be done here.
Isn't it convenient?

< PHP
$cache->end ();//This must not be forgotten,
Otherwise the cache is never built successfully,
This function is out of the output buffer.
}
?>

And see if there's another file in the cache directory?
The cache is introduced here, here are a few points to note:
1.cache ID must be unique, you can integrate some parameters in the
2.cache Cache path Best Write absolute path
3. The focus of this section is on annotations, please read the notes carefully

In addition, through my introduction, how to delete the cache you can go to see the corresponding manual, the above example is to stimulate, if the above example you will use, the next cache operation should not be difficult.

http://www.bkjia.com/PHPjc/486625.html www.bkjia.com true http://www.bkjia.com/PHPjc/486625.html techarticle the previous introduction of pear installation, today began to introduce pear one of several well-known package, if there is unclear place please stand inside search "Pear Tutorial" get the previous tutorial! Today we ...

  • 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.