Content Cache Output PEAR Cache
Next we start 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.
The following is a PHP applet that does not use caching:
pear_content_cache1.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, and now we add caching to it.
pear_content_cache2.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 single cache ID
Request +cookie Information
$cache _id = $cache->generateid (array (' URL ' => $_request, ' post ' =>$_post, ' cookies ' => $HTTP _cookie_vars));
}
Else
{
To get the latest content, the ID is empty
$cache _id = null;
}
See if Cache ID corresponds to cached content 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.
";
echo "Current time is". Date (' M-d-y h:i:s A ', Time ()). "
";
Write content to cache
echo $cache->end ();
? >
Refresh the two files individually, and you will see that the time in the pear_content_cache1.php "Current time" in the line changes with the refresh, and the line in the 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.
4. Create an output cache object
$cache = new Cache_output (' file ', array (' Cache_dir ' => $cacheDir));
The first parameter indicates that we use a cache based on the file, and the second parameter is an array associated with the cached directory.
5. Generate a unique cache ID
$cache _id = $cache->generateid (array (' URL ' => $_request, ' post ' =>$_post, ' cookies ' => $HTTP _cookie_vars));
Here the $cache object's Generateid () method uniquely identifies the request by providing an array of information (URL, HTTP POST data, and HTTP cookies), which distinguishes it from other requests.
6. Add a logical judgment statement to see if the cached data for CacheID already exists, and if so, get the data and end the script.
if ($content = $cache->start ($cache _id))
{
Echo $content;
Exit ();
}
7. Place the resulting code after the above logical statement and end the use of the cache object.
echo $cache->end ();
function Cache Output PEAR cache
In addition to caching the content of the output, pear can also cache the results of a call to a function. This is a very interesting feature, if your program is going to use a function frequently, and the result of the call is the same, I suggest you try it, especially if the function is slow to run.
Here we implement a buffer call to a function slowfunction () that is slow to execute.
Require_once ' cache/function.php ';
$cacheDir = './pear_cache/';
$cache = new Cache_function (' file ', array (' Cache_dir ' => $cacheDir));
$arr = Array (' Apple ', ' pear ', ' watermelon ');
$cache->call (' slowfunction ', $arr);
Echo '
';
$arr = Array (' Apple ', ' pear ', ' watermelon ');
Slowfunction ($arr);
function slowfunction ($arr = null)
{
echo "A function that is slow to perform:(
";
echo "Current time is". Date (' M-d-y h:i:s A ', Time ()). '
';
foreach ($arr as $fruit)
{
echo "I ate a $fruit
";
}
)
? >
The following is the script execution result for the sample:
A function that is slow to execute
Current time is jul-28-2004 17:15:57 PM
I ate an apple.
I ate a pear.
I ate a watermelon.
A function that is slow to perform:(
Current time is jul-28-2004 17:17:55 PM
I ate an apple.
I ate a pear.
I ate a watermelon.
Code, the Cache/function.php class is used to perform function buffering functions. $cache variable is a Cache_function object that is stored in the $cachedir directory using a file-based function cache. To cache a function tune, the call () method of the Cache_function object $cache is used like this: $cache->call (' slowfunction ', $arr);
Here, the slowfunction () function is called, and the parameter is an array $arr, which is cached in a file in the $cachedir directory. Any call to this function after this will be returned by $cache->call () to the result of the function execution. function caching is similar to using methods and content caching, no more, please see the Pear Manual.