Using PHP to implement database query result caching

Source: Internet
Author: User

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

The reason for caching

The 1th first look at our normal situation to execute a SQL query overhead, we first connect the database, and then prepare the SQL query, then send the query information, and then get the return results, and finally close the database connection, so that will occupy more resources, Our PHP program also slows down the response because it is waiting to be queried from the database.

The 2nd is when the database pressure is large, such as peak period, this time the database pressure, we need to store some data on the hard disk, the form of files to read, this approach is to use our hard disk space in exchange for database pressure, this point also depends on the machine performance.

The 3rd is that some data is not anxious to update, such as the above mentioned commodity type table, will not be too eager to update, such as our users of the core information, generally will not easily modify the password or something, these content can choose to file the form of caching.

The implementation principle of caching

The 1th is that we want to determine when to force updates, the most common there are three ways is the first is to use time to trigger, we usually use the timestamp, the 2nd is to find that the database data has been modified, then automatically update the cache, the third is artificial trigger, we use artificial waterproof tell information system forced to update the cached 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 the unserialize, so the so-called serialization is to store the PHP value in a specific way, It will ensure that the department loses the type and structure of these values.

Actual demo

We first store the data read from the database in the local file, the code is as follows:

 -->
Then we can open the Sqlcache.txt file, which reads as follows:

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: "Daiju Narita"; 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: "Brother Xing"; 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 simulate the use of the cache.


Description:

1. We get the creation time of the file by Filemtime, we can use time to now, by comparing the difference to decide whether to update the cache.

2. We can use unlink to force the deletion of the file to empty the data cache





'

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.