Can PHP not back cache line? Multiple implementations

Source: Internet
Author: User
Will PHP not return cache lines? Multiple implementations

1. Universal Cache Technology:

Data cache: In this case, the data cache refers to the database query PHP cache mechanism, each time you visit the page, will first detect the corresponding cache data exists, if not exist, connect to the database, get the data, and the query results are serialized and saved to the file, Later, the same query results are obtained directly from the cache table or file.

The most widely used example is the search function of discuz, which caches the result ID into a table and searches the cache table the next time the same keyword is searched.

For a common method, multi-table association, the schedule of the contents of the array is saved to a field in the main table, the need for the array decomposition, the advantage is only read a table, the disadvantage is that two data synchronization will be more than a few steps, the database is always the bottleneck, with the hard disk speed, is the key point.

2. Page cache:

Each time you visit the page, will detect the corresponding cache page file exists, if not exist, connect to the database, get data, display the page and generate the cache page file at the same time, so the next visit to the page file will play a role. (the template engine and some common PHP caching mechanism classes on the web usually have this feature)

3. Time-Triggered cache:

  Check that the file exists and that the timestamp is less than the expiration time of the setting, and if the timestamp of the file modification is greater than the current timestamp minus the expiration timestamp, then cache is used, otherwise the cache is updated.

4. Content Trigger Cache:

  Forces the PHP cache mechanism to be updated when data is inserted or updated.

5. Static cache:

Static caching refers to static, directly generated HTML or XML and other text files, there is an update when re-generated once, suitable for the less changing pages, this does not say.


6. Memory Cache:

Memcached is a high-performance, distributed memory object PHP caching mechanism system for reducing database load and increasing access speed in dynamic applications.

7, the buffer of PHP:

  There are eaccelerator, APC, Phpa,xcache, this is not to say, search a bunch of a bunch of, see for yourself, know that this thing is OK

8. mysql Cache:

This is also non-code level, the classic database is used in this way, look at the following running time, 0.09xxx and the like

9. Reverse proxy-based Web caching:

  such as Nginx,squid,mod_proxy (Apache2 and above are divided into mod_proxy and Mod_cache)

10. DNS Polling:  
Bind is an open source DNS server software, this to say it is big, their own search, you know there is this thing on the line.
I know that there are chinacache and other major stations to do so, said simple point is a multi-server, the same page or file cache to different servers, according to the North-South automatic resolution to the relevant server

For the fifth method, the case is as follows:

Code:

1. Create a test database

Create Database LXR;

Use LXR;

CREATE TABLE News (

ID int unsigned primary key auto_increment,/* news id*/

title varchar (+) not null,/* news headlines * /

Content varchar Not null,/* News contents * /

filename varchar (+)/* record static page name */engine=myisam

2. test Data

INSERT INTO News (title,content) VALUES (' Hello1 ', ' hello Beijing ');

INSERT INTO News (title,content) VALUES (' Hello2 ', ' hello Sichuan ');

The news_list.php code is as follows:

// News list

$conn =mysql_connect ("localhost", "root", "root");

if (! $conn) {

Die (" Connection failed ");

}

mysql_select_db ("LXR", $conn);

mysql_query ("Set names UTF8");

$sql = "SELECT * FROM News";

$res =mysql_query ($sql);

Header ("Content-type:text/html;charset=utf-8");

echo "

News list

";

echo " Add News

";

echo "

echo "

while ($row =mysql_fetch_assoc ($res)) {

echo "

}

echo "

"; "; ";
Id Title View Details
{$row [' id ']} {$row [' title ']} View Details
";

// Close Resources

Mysql_free_result ($res);

Mysql_close ($conn);

?>

show_news.php

// accept ID

$id =@$_get[' id '];

// See how to use HTML static pages

// train of thought to see if the HTML page has, if any, direct access, no on create

// Build a file name .

$html _filename= "news_id". $id. ". HTML ";

//filemtime () = Last modified time to get the file

//filemtime ($html _filename) +30>time () represents a static file,

if (file_exists ($html _filename) && filemtime ($html _filename) +30>time ()) {

// direct access to HTML page ( HTML page content echo browser )

Echo file_get_contents ($html _filename);

Exit

}

$conn =mysql_connect ("localhost", "root", "root");

if (! $conn) {

Die (" Connection failed ");

}

mysql_select_db ("LXR", $conn);

$sql = "SELECT * from news where id= $id";

$res =mysql_query ($sql);

// open ob cache

Ob_start ();

if ($row =mysql_fetch_assoc ($res)) {

Header ("Content-type:text/html;charset=utf-8");

echo "

echo "

echo "

echo "

echo "

"; "; "; ";
News Detailed Content
{$row [' title ']}
{$row [' content ']}
";

}else{

echo " no results ";

}

$html _content=ob_get_contents ();

$my _hader= " ";

// put ob-> $html _filename ( if necessary, take the path into account )

File_put_contents ($html _filename, $my _hader. $html _content);

Mysql_free_result ($res);

Mysql_close ($conn);

?>

Usage: The SQL script above will be used to create a good database and table. Put the above two PHP files in your development environment. Run the first news_list.php to generate a static page.

I summarize as follows: In fact, the two functions Ob_start (); Ob_get_contents (); Place the part you want to cache between the two functions, and the cached content is the return value of the Ob_get_contents function.

and File_put_contents ($html _filename, $my _hader. $html _content); Writes the cached page to the file,

if (file_exists ($html _filename) && filemtime ($html _filename) +30>time ()) {

// direct access to HTML page ( HTML page content echo browser )

Echo file_get_contents ($html _filename);

Exit

}

Read the time to see if the file exists and set a delay time, if there is a direct echo out, if not exist, then manipulate the database and write to the file.

This is the caching mechanism of PHP itself. In fact, there are a lot of technology, need to digest.

Top One, pro ...

  • 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.