The clever use of Cache Technology in Web development not only reduces the server pressure, but also increases the browsing speed. The following describes how to use the Cache Technology in PHP to cache pages.
Most websites are database-based dynamic pages. That is to say, your page is equivalent to an application that obtains data from a database system (such as MySQL), parses the data, and then presents it to the user. Most of the data is not updated frequently. The reason we use the database is that it is very convenient to update the data and content.
A large number of database connections and queries can overload or paralyze the server. Each time a database is queried, the script links to the DBMS once, And the DBMS returns the query result. This is a waste of time and resources. If the frequency is very high, database errors may occur.
How can this problem be solved?
There are two ways to solve this problem. One is to optimize the query, but this is not discussed in this article; the other is to use the cache.
Use Cache Technology
Let me explain it. When we have a dynamic page with data updates that are not frequently updated, we can use the 'system' method to create the page and save it for later use. That is to say, after the page is created, as long as it does not expire, you do not need to query the database again, but only display the cache page. Of course, the system must set an expiration time.
The following is a sample code.
<? PHP
Class Cache
{
VaR $ cache_dir = './tmp/Cache/'; // This is the directory where the cache files will be stored;
VaR $ cache_time = 1000; // how much time will keep the cache files in seconds.
VaR $ caching = false;
VaR $ file = '';
Function cache ()
{
// Constructor of the class
$ This-> file = $ this-> cache_dir. urlencode ($ _ server ['request _ URI ']);
If (file_exists ($ this-> file) & (fileatime ($ this-> file) + $ this-> cache_time)> time ())
{
// Grab the cache:
$ Handle = fopen ($ this-> file, "R ");
Do {
$ DATA = fread ($ handle, 8192 );
If (strlen ($ data) = 0 ){
Break;
}
Echo $ data;
} While (true );
Fclose ($ handle );
Exit ();
}
Else
{
// Create cache:
$ This-> caching = true;
Ob_start ();
}
}
Function close ()
{
// You shoshould have this at the end of each page
If ($ this-> caching)
{
// You were caching the contents so display them, and write the cache file
$ DATA = ob_get_clean ();
Echo $ data;
$ Fp = fopen ($ this-> file, 'w ');
Fwrite ($ FP, $ data );
Fclose ($ FP );
}
}
}
// Example:
$ CH = new cache ();
Echo date ("d m j g: I: S T Y ");
$ Ch-> close ();
?>
The code function is described as follows:
Function cache ()
This is the constructor of this class. This function is used to check whether a cache page exists or whether the cache page has expired and is created. The following is how it is implemented:
$ This-> file = $ this-> cache_dir. urlencode ($ _ server ['request _ URI ']);
This Code creates a target file, which is like this:/path/to/Cache/DIR/request_uri
If (file_exists ($ this-> file) & (fileatime ($ this-> file) + $ this-> cache_time)> time ())
This code checks whether a cache page exists, or you need to recreate the cache page because it may have expired. If the cache page still has a shelf life, the cache page is displayed and then exited. I will explain why I quit. If the cache page must be rebuilt, the following code will take effect.
$ This-> caching = true;
Ob_start ();
The first sentence indicates starting to create a cache page. The second sentence begins to cache, And the cached data will be used when the close () function is called.
Function close ()
This function must be called at the end of the script to complete the final work.
The following explains how this function works.
$ DATA = ob_get_clean ();
Here we get all the cache content before calling this function, delete the cache, and assign the value to $ data.
This is a very simple class that aims to understand the cache and better apply it to your website. To use this class, the following form is required:
<? PHP
$ A = new cache ();
....
....
....
$ A-> close ();
?>
If there is code after $ A-> close (), the code will be invalid. Because there is exit in the cache () function. A better solution is to remove the exit function from the cache function, and then use
<? PHP
$ A = new cache ();
If ($ A-> caching)
{
....
....
....
}
$ A-> close ();
?>
Skilled in using the cache technology, you can get twice the result with half the effort!
This article is from [<a href = 'HTTP: // www.houxd.com 'target =' _ blank '> post-modern network resource portal </a>]! Detailed source reference: http://www.lindsoft.net/html/net_tech/php/rmCXyKWZtKK0sC2w.htm