◆ Content Cache Output Pear Cache
Next we start to explore more commonly used cache technology, this is also a key part of this article. First we use the Cache package in PEAR. PEAR can cache content in files, databases, or memory, and we take document as an example.
Here is a PHP applet without cache:
Pear_Content_Cache1.php
php echo "This is the content.
"; echo "Current time is". DATE ('m-d-y h: i: s a', time ()). "
";?>
The above program is very simple, now we add a cache.
Pear_Content_Cache2.php
PHP Require_once 'Cache / Output.php'; // Set the 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 content in the cache // If you want to get the latest content, you must assign a value to the NOCACHE variable IF (Empty ($ _ Request ['Nocache']) {// Establish a Unique Cache Identity // 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 if the cache ID corresponds to whether the cache content can be available ($ content = $ cache-> start ($ cache_id)) {// cache already exists Direct output, and end the script echo $ content; exit ();} // cache does not exist in this content, generate new content and write cache echo "This is content.
"; echo "Current time is". Date ('MDY H: I: S A', TIME ()). "
";
// Write the content to Cache Echo $ cache-> end ();?>
Refresh these two files, you will find that "Current Time" in Pear_Content_Cache1.php is changed in this line as refreshed, and this line in Pear_Content_Cache2.php does not change. This is because Pear_Content_Cache2.php uses a cache to store the contents of the user in a static file. When the user requests again, it outputs directly from the file without the need to dynamically generate content.
For Pear_Content_Cache2.php, if the user wants to read the latest information, not the older information in the cache. Then you can use http: //.../pear_content_cache2.php? Nocache = 1 to access the cache function. Refresh and see, you will find the time will change.
Summarize the use of PEAR content cache classes:
1. Contains a PEAR package to pay attention to the path.
2. Contains Cache classes in Output.php
Require_once 'cache / output.php';
3. Set the cache directory
$ cachedir = './pear_cache';
Confirm that this directory is writable. Cache data will be written to the subdirectory of this directory.
4. Create an output cache object
$ cache = new cache_output ('file', array ('cache_dir' => $ cached);
The first parameter indicates that we use the "file"-based cache, and the second parameter is an array associated with the cache directory.
5. Generate a unique cache ID
$ cache_id = $ cache-> generateid (Array ('url' => $ _REQUEST, 'POST' => $ _ POST, 'Cookies' => $ http_cookie_vars);
The generateId () method of the $ CACHE object is unanimously identified by providing an information array (URL, HTTP POST DATA, and HTTP cookie), and is separated from other requests.
6. Add a logical judgment statement to see if the cache data corresponding to the CacheID already exists, if there is, the data is obtained and end the script.
IF ($ Content = $ Cache-> Start ($ Cache_ID)) {Echo $ Content; Exit ();
7. Place the code generated after the above logical statement, and end the use of the Cache object.
Echo $ cache-> end ();