In phpmysql web applications, we often encounter tens of millions of data records. to reduce the server load, we often use the third tool to reduce the pressure, next we will provide you with a Memcachephp method to improve mysql load efficiency. mem... in php mysql web applications, we often encounter tens of millions of data records. to reduce the server load, we often use the third tool to reduce the pressure, next we will provide you with a Memcache php method to improve mysql load efficiency.
Reason for Memcache:
1. web Server (Lighttpd and Nginx are said to be much more efficient than Apache, so you can try them out) has high CPU requirements and low memory requirements, while Memcached Server has low CPU requirements, high memory requirements, so it can be used together. it is feasible to install Memcached Server on the front-end Web Server.
2. Money, money, and money. the minimum amount of money is paid to maximize the benefits.
3. simple and simple. for a system with a reasonable architecture, adding Memcache support may be only a process of batch file processing.
Discuz! Use Memcache
1. add the following code to config. inc. php:
$ Memcachehost = '2017. 0.0.1 ';
$ Memcacheport = 11211;
$ Memcachelife = 60;
2. in include/common. inc. php
$ Mem = new Memcache;
$ Mem-> connect ($ memcachehost, $ memcacheport );
3. modify the fetch_array and query methods in include/db_mysql.class.php and add the query_mysql method. the code is as follows:
get($key))) { $query = $this->query($sql, $type); while ($item = $this->fetch_array($query)) { $res[] = $item; } $query = $res; $mem->set($key, $query, 0, $memcachelife); } return $query;}function query($sql, $type = '') { global $debug, $discuz_starttime, $sqldebug, $sqlspenttimes; $func = $type == 'UNBUFFERED' && @function_exists('mysql_unbuffered_query') ? 'mysql_unbuffered_query' : 'mysql_query'; if (!($query = $func($sql, $this->link)) && $type != 'SILENT') { $this->halt('MySQL Query Error', $sql); } if (substr($sql, 0, 6) == 'SELECT') { echo 'Cache SQL:' . $sql . '
'; } else { echo 'Flash SQL:' . $sql . '
'; } $this->querynum++; return $query;}?>
4. change the code of the SQL query that needs to be cached by using Memcache from $ db-> query (to $ db-> query_memcache (note that while ($ post = $ db-> fetch_array ($ query )) {change to foreach ($ query as $ post ){
$ Db-> fetch_array without while can be modified.