How Memcachephp improves mysql load efficiency

Source: Internet
Author: User
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.

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.