PHP Implementation database Data read generate cache file

Source: Internet
Author: User

Sometimes we want to reduce the database query to improve the performance of the program, because the data is not often changed, but it will not change for a long time, so we each connected database, will be the corresponding results in the form of files saved. For example, for a mall, the quantity of our goods may change often, but our commodity type and the price of the goods will not change for a long time, if we need to query them frequently, we can use the database caching technology.

Reason for caching

1th First look at our normal situation to execute a SQL query overhead, we first connect to the database, then prepare the SQL query, then send the query information, and then get the results returned, and finally close the database connection, so that will occupy more resources, Our PHP program also slows down the response by waiting for queries from the database.

2nd, when the database pressure is large, such as peak hours, this time the database pressure, we need to put some data to the hard disk, the form of files to read, so that the practice is to use our hard disk space in exchange for database pressure, this also depends on the performance of the machine.

3rd, some data is not anxious to update, such as the above mentioned commodity type table, will not be too eager to update, such as the core information of our users, generally will not easily change the password or something, these content can choose to use the form of files to cache up.

How the cache is implemented

1th, we want to determine when to force the update content, the most common three ways is the first is to use time to trigger, we usually use time stamp, the 2nd is to find that the database data is modified, automatically update the cache, the third is the manual trigger, we use the artificial waterproof tell information System Force update cache content.

The 2nd is that we can use the Serialize () function to serialize the data obtained from the database, save it as a local file, and then we read the information from the local file through Unserialize, so that serialization is a specific way to store PHP values. It will ensure that the department loses the type and structure of these values.

Actual Demo

We first put the data read from the database into a local file, the code is as follows:

Mysqli_connect ("localhost", "root", "" "," BBS ");//The second step is to set the corresponding character encoding $setting = ' Set names UTF8 '; Mysqli_query ($conn, $setting) ;//The third step is to query $sql = ' SELECT * from user '; $result = Mysqli_query ($conn, $sql);//The fourth step turns the query result into an array $rows = Mysqli_num_rows ($ result); $sqldata = Array (); for ($i = 0; $i < $rows; $i + +) {$sqldata [] = Mysqli_fetch_assoc ($result);} The fifth step writes the result to the cache file $file = "Sqlcache.txt"; $msg = serialize ($sqldata); $fp = fopen ($file, "w"); Fputs ($fp, $msg); fclose ($FP);


We can then open this Sqlcache.txt file with the following contents:

A:6:{i:0;a:4:{s:2: "id"; s:1: "1"; S:5: "Level"; s:1: "0"; s:4: "Name"; s:6: "Sinsing"; s:3: "pwd"; s:32: " Bd04fcc97578ce33ca5fb331f42bc375 ";} I:1;a:4:{s:2: "id"; s:1: "2"; S:5: "Level"; s:1: "1"; s:4: "Name"; s:6: "Xiao Qian"; s:3: "pwd"; s:32: " 61cb72858be523b9926ecc3d7da5d0c6 ";} I:2;a:4:{s:2: "id"; s:1: "3"; S:5: "Level"; s:1: "1"; s:4: "Name"; s:6: "Xiao Nan"; s:3: "pwd"; s:32: " a3d2de7675556553a5f08e4c88d2c228 ";} I:3;a:4:{s:2: "id"; s:1: "4"; S:5: "Level"; s:1: "1"; s:4: "Name"; s:6: "Liu Qiang"; s:3: "pwd"; s:32: " Fcdb06a72af0516502e5fdccc9181ee0 ";} I:4;a:4:{s:2: "id"; s:1: "5"; S:5: "Level"; s:1: "1"; s:4: "Name"; S:6: "star brother"; S:3: "pwd"; s:32: " 866a6cafcf74ab3c2612a85626f1c706 ";} I:5;a:4:{s:2: "id"; s:1: "6"; S:5: "Level"; s:1: "1"; s:4: "Name"; s:6: "Sin Yong"; s:3: "pwd"; s:32: " E93beb7663f3320eaa0157730d02dd0c ";}}

Then we can write a program to read the data from the file, the PHP code is as follows:

So our $result is the data read from the local TXT file, not the data read from the database, that is, we simulated the use of the cache.


Description

1. We use the filemtime to get the creation time of the file, which can be used to present time, by comparing this difference to determine whether to update the cache.

2. We can use unlink to force the deletion of files to clear the data cache


PHP Implementation database Data read generate cache file

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.