Reasons for Memcache:
1.Web Server (LIGHTTPD, Nginx is said to be more efficient than Apache, you can try) on the CPU requirements high, low memory requirements, and memcached Server is low CPU requirements, high memory requirements, so you can use. It is possible to install Memcached server on the front-end Web server.
2. Money, money, money, the least pay, get the most benefits.
3. Simple and simple, for a system with reasonable architecture, adding memcache support may only be a process of handling files in batches
discuz! Use Memcache
1. Increase in the number of config.inc.php
| The code is as follows |
Copy Code |
| $memcachehost = ' 127.0.0.1 '; $memcacheport = 11211; $memcachelife = 60; 2. In the 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 following code: function Fetch_array ($query, $result _type = Mysql_assoc) { Return Is_resource ($query)? Mysql_fetch_array ($query, $result _type): $query [0]; } function Query_memcache ($sql, $type = ' ") { Global $mem, $memcachelife; $key = MD5 ($sql); if (!) ( $query = $mem->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 ' <font color= "red" >cache sql</font>:<font color= "green" > '. $sql. ' </font><br/><br/> '; } else { Echo ' <font color= "red" >flash sql</font>:<font color= "green" > '. $sql. ' </font><br/><br/> '; } $this->querynum++; return $query; } |
4. The code for the SQL query that will need to use the Memcache cache is
$db->query (
Amended to
| The code is as follows |
Copy Code |
| $db->query_memcache ( |
Note and put
| The code is as follows |
Copy Code |
| while ($post = $db->fetch_array ($query)) { |
Amended to
| The code is as follows |
Copy Code |
| foreach ($query as $post) { |
$db->fetch_array without a while can be modified without modification.
The following code is useful to take it:
Preg_replace ("/while" ([$] (w+) S*=s*[$]db->fetch_array ([$]query))/is "," foreach ($query as $\1) ", $file);
Turn off a small tool to replace the batch.
This can be replaced in EditPlus: while ([$] (. *) = [$]db->fetch_array ([$]query)) replaced with foreach ($query as $)