PHP program accelerates the discovery of cached output

Source: Internet
Author: User
Tags date current time empty pear
Program | Cache Content cache output PEAR Cache

Next we begin to explore more commonly used caching techniques, which is also a key part of this article. First we use the cache package in pear. Pear can cache content in a file, in a database, or in memory, as an example of a file.





below is a PHP applet that does not use caching:





pear_content_cache1.php


  


<?php


echo "This is the content. <p> ";


echo "Current time is". Date (' M-d-y h:i:s A ', Time ()). "<BR>";


? >


The above program is very simple, now we add caching to it.





pear_content_cache2.php





<?php


require_once ' cache/output.php ';





//Set cache directory, must be writable


$cacheDir = './pear_cache ';


$cache = new Cache_output (' file ', array (' Cache_dir ' => $cacheDir));





//If the NoCache variable is empty, use the contents of the cache


//If you want to get the latest content, assign the value to the NoCache variable


if (Empty ($_request[' NoCache '))


 {


//Create a unique cache ID


//Request +cookie Information


$cache _id = $cache->generateid (array (' URL ' => $_request, ' post ' =>$_post, ' cookies ' => $HTTP _cookie_vars));


 }


Else


 {


//Want to get the latest content, ID is empty


$cache _id = null;


 }





//See cache ID corresponding to the cached content is available


if ($content = $cache->start ($cache _id))


 {


//cache already exists, direct output, and end script


Echo $content;


exit ();


 }




The content does not exist in the
//cache, generating new content and writing to the cache


echo "This is the content. <p> ";


echo "Current time is". Date (' M-d-y h:i:s A ', Time ()). "<BR>";





//write content to cache


echo $cache->end ();


? >


refresh these two files separately, you will find that the time in the line "current time is" in pear_content_cache1.php changes with the refresh, and the line in pear_content_cache2.php is unchanged. This is because pear_content_cache2.php uses a cache to store the content requested by the user in a static file. When the user requests it again, it outputs directly from the file without the need to dynamically generate the content with the program.





For pear_content_cache2.php, if the user wants to read the latest information, not the old information in the cache. Then you can use http://.../pear_content_cache2.php?nocache=1 to access it, which disables caching. Refresh and see, you will find that time will change.





summarize the use of the Pear content cache class:





1. Include the pear package to be aware of the set path.


 


2. Contains the cache class in output.php





require_once ' cache/output.php ';


3. Set Cache directory





$cacheDir = './pear_cache ';


Verify that the directory is writable. The cache data will be written to the subdirectory of this directory.

[1] [2] Next page







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.