PHP uses memcache to accelerate MySQL speed and increase database load

Source: Internet
Author: User
Tags memcached php server

Memcached is a high-performance distributed memory object caching system for dynamic Web applications to mitigate database load. It improves the speed of dynamic, database-driven Web sites by caching data and objects in memory to reduce the number of times a database is read.

———— excerpt from AMH panel introduction

Blog Station server with the AMH of the panel did not compile their own PHP, such as the Application Center directly installed the Memcache server

If you compile with your own http://memcached.org/to download the installation on the official website, here will not say more ~

After installation, ensure that the PHP server supports memcache extension, if not supported, you can install the dependent library to the following two addresses

Https://launchpad.net/libmemcached/1.0/1.0.4/+download/libmemcached-1.0.4.tar.gz

Http://pecl.php.net/get/memcached-2.0.1.tgz

After installation phpinfo () Check to see if the Memcache feature is supported and the next step if supported

Memcache not modified should be used with the default port 11211

First Test can be used

$mem = new Memcache;
$mem->connect ("127.0.0.1", 11211);
$mem->set (' Test ', ' OK ', 0,60);
$mem->get (' Test ');

PHP executes the above code, if the output OK then the program is normal, can be used.

My idea is basically this, first determine whether the current data is stored in the Memcache database, if there is a direct output, does not exist call MySQL data and store the output.

This basically can achieve the storage of data, I ihxvip this is achieved, the following to a similar query chestnut bar, using mysqli

First, the first step is to create

$mem = new Memcache;
$mem->connect ("127.0.0.1", 11211);

Then direct if data is present

if ($mem->get ($data) = = ") {

equals NULL to start using the database and store it in Memcache

$db = new mysqli (sqlhost,sqluser,sqlpass,sqldata);
$db->query ("Set names ' UTF8 '");
$rows = $db->query ("SELECT * from test where id = data");
if ($row = $rows->fetch_array ()) {
$mem->set ($data, $row, 0, 300);
}

After processing, almost on the line, anyway this thing is put in the program head run, need data, directly use Get get $mem->get ($data) can be

If you want to be convenient, you can put into a function, anyway, basically I do so, use please modify, here but the idea

But with the memcache speed really did not say, processing speed than MySQL more than 6

PHP uses memcache to accelerate MySQL speed and increase database load

Related Article

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.