Content Cache Output PEAR Cache
The next step is 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, database, or memory, for example.
Here is a PHP applet that does not use the cache:
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 are adding a cache for 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 a 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 cached content for cache ID 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.
";
echo "Current time is". Date (' M-d-y h:i:s A ', Time ()). "
";
Write content to cache
echo $cache->end ();
? >
To refresh these two files separately, you will find that the time in the pear_content_cache1.php in this row is changed with the refresh, and this line in pear_content_cache2.php is unchanged. This is because pear_content_cache2.php uses the cache to store the user's requested content in a static file. When the user requests it again, it outputs directly from the file without requiring the program to dynamically generate the content.
For pear_content_cache2.php, if the user wants to read the latest information instead of the old information in the cache. Then you can use http://.../pear_content_cache2.php?nocache=1 to access it, which disables the caching feature. Refresh your look and you'll see that time will change.
Summarize the use of the Pear content cache class:
1. Include the pear package to be aware of the set-to path.
2. Contains the cache class in output.php
Require_once ' cache/output.php ';
3. Set the cache directory
$cacheDir = './pear_cache ';
Verify that the directory is writable. The cache data will be written to subdirectories in 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 "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));
Here the $cache object's Generateid () method uniquely identifies the request by providing an array of information (URL, HTTP POST data, and HTTP cookie), distinguishing it from other requests.
6. Add a logical judgment statement to see if the cache data that corresponds to CacheID already exists, and if so, gets the data and ends the script.
if ($content = $cache->start ($cache _id))
{
Echo $content;
Exit ();
}
7. Put the generated content code behind 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 output content, pear can also cache the invocation results of a function. This is a very interesting feature, and 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.
Below we implement a buffered 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 execute:(
";
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 a sample script execution result:
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 execute:(
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. The $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 subsequent call to this function will be returned by $cache->call () to the result of the function's execution. The function cache and the use method and content cache are very similar, no longer say more, see the Pear Manual for details.
http://www.bkjia.com/PHPjc/372079.html www.bkjia.com true http://www.bkjia.com/PHPjc/372079.html techarticle Content Cache Output PEAR Cache the next step is to explore the more common caching techniques, which are the focus of this article. First we use the cache package in pear. Pear can cache content in ...