Sometimes we want to reduce queries to the database to improve program performance, because these data do not change frequently, but will not change for a long time. Therefore, every time we connect to the database, we will The corresponding results are saved in the form of files. For example, for a mall, the number of our products may change frequently, but our product types and product prices will not change for a long period of time. If we need to query them frequently, You can use database caching technology.
Caching reasons
The first point is to first look at the overhead of executing a SQL query under normal circumstances. We first connect to the database, then prepare the SQL query, then send the query information, then obtain the returned results, and finally close the database connection. This will take up a lot of time. A lot of resources, and our PHP program also slows down the response because it has to wait for queries from the database.
The second point is that when the database pressure is high, such as peak hours, when the database pressure is high, we need to store some data on the hard disk and read it in the form of files. This approach is to use our Hard disk space is exchanged for database pressure, which also depends on machine performance.
The third point is that there is no rush to update some data. For example, the product type table mentioned above is not too eager to update. For example, the core information of our users generally does not change passwords easily. These Content can optionally be cached in the form of files.
The implementation principle of caching
The first point is that we need to determine when to force the content to be updated. There are three most common ways. The first is to use time to trigger. We usually use timestamps. The second point is to automatically detect that the database data has been modified. To update the cache, the third step is manual triggering. We use artificial waterproofing to tell the information system to forcefully update the cache content.
The second point is that we can use the serialize() function to serialize the data obtained from the database and save it as a local file. Then we use unserialize to read information from the local file. The so-called serialization is to use A specific way to store PHP values that ensures that the type and structure of those values are not lost.
Practical demonstration
We first store the data read from the database into a local file. The code is as follows:
<?php //第一步连接数据库 $conn = mysqli_connect("localhost","root","","bbs"); //第二步设置相应的字符编码 $setting = 'set names utf8'; mysqli_query($conn,$setting); //第三步进行查询 $sql = 'SELECT * FROM user'; $result = mysqli_query($conn,$sql); //第四步把查询结果转化为一个数组 $rows = mysqli_num_rows($result); $sqldata = array(); for($i = 0;$i <$rows;$i ++){ $sqldata[] = mysqli_fetch_assoc($result); } //第五步把结果写到缓存文件 $file = "sqlcache.txt"; $msg = serialize($sqldata); $fp = fopen($file,"w"); fputs($fp,$msg); fclose($fp);
a:6:{i:0;a:4:{s:2:"id";s:1:"1";s:5:"level";s:1:"0";s:4:"name";s:6:"辛星";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:"小倩";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:"小楠";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:"刘强";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:"星哥";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:"辛勇";s:3:"pwd";s:32:"e93beb7663f3320eaa0157730d02dd0c";}}
<?php $file = "sqlcache.txt"; $msg = file_get_contents($file); $result = unserialize($msg); var_dump($result);
In this way, our $result is the data read from the local txt file instead of the data read from the database, that is, we simulate the use of cache.
Description:
1. We use filemtime to get the creation time of the file. We can use time to get the current time, and decide whether to update the cache by comparing the difference.
2. We can use unlink to forcefully delete files to clear the data cache