Memcached+php+mysql+linux Practice

Source: Internet
Author: User
Tags memcached


First make sure your server environment already has memcached and lamp, on the Linux build memcahced+php environment can refer to my other post
(http://www.cnblogs.com/codeAB/p/5591118.html)
You should take these questions with you before you look down.
{memcached can solve any problem} {Now a topic of hot discussion Radis replace memcached}
{What is the appropriate use of memcached} {memcached Basic usage} {Build a distributed cache system with memcached}

(1) For example, the forum in front of a few are the essence of posts, the largest number of visits. A post includes [the body of the post. Comments. ...], let's simulate the data, a post visited 1 million times a day, commented 100,000 times, and liked 500,000 times. If all the operations are connected directly to MySQL, how many database queries do you need to execute at least?
(post 1 million times-read, review 100,000 Times-write, like 500,000 times-write), a post at least 1.6 million times a day to perform database operations, and now we use memcached to alleviate the MySQL pressure,
When the first user accesses this post, the data is cached to memcached, the cache includes the thread body, comments (usually only the last dozens of comments are cached), and the number of likes, so that the second person clicks
This post, directly from the memcached to get the data back, there are new comments to update the comment data to memcached, like is also directly updated memcached, even after the post content was modified by the publisher
You can also directly modify the contents of the memcached, do not have to move the database, all the updates and reads are done in memcached, that is, from a previous post 1.6 million times a day database operations into a!! As long as your server is constantly on, the data can last one months, one months a post from 48 million database operations---> into 1 times. Of course, this concludes that 1 times is wrong, because the maximum cache time of the memcached is 30 days, if the server power outage, then the data will be lost, so in just this example, for comments on the increase and the content of the post changes, these changes will eventually have to be stored in the database good, So I suggest that you can use the timer (Linux Contrab) every few minutes to synchronize the latest memcahced data to the database, even if the server power off, but also lost only a few minutes of data, it is acceptable, of course, the server for no reason the probability of power loss is very small.
I would like to compare this with the above example you should see what memcached can solve. Mainly used to alleviate the database pressure.

(2) Now a lot of people talk about or already in practice using Radis (refer to NoSQL) to replace the memcached, here I do not long-winded to say the difference between the two, you can search the relevant article information,
Picking up a few key points, Memcached's goal is to do the cache, in order to alleviate the pressure of the database, and Radis the goal is to do a database replacement, the goods are built in the memory of the database, memcached after the power loss (can be persisted with the timer), Radis is a persistent database, the most important point is: memcached single lightweight, simple deployment, multi-threaded distributed, Radis comparison eating configuration, and is single-threaded, see in the large distributed cache system when the memcached more convenient and secure,
Google and Facebook are all using memcached, while domestic use of Radis is also more. Finally we look at the difference between the 1th goal, one is to alleviate the database pressure, one is to replace the traditional relational database of NoSQL,
That is to say memcached and Radis is not a competitive relationship, even the two can work together, Radis as a database, memcached as the intermediate cache system, alleviate radis load, and finally I hope
You also learn about NoSQL, a high-performance memory database, I believe this is a future development of the database direction!

(3) memcached itself is very simple to use, I believe you spend 10 minutes to read the official documents to know how each method has a role, but after reading is not a blank face?
Is it appropriate to use memcached in those situations? First different site different scenes under a fixed rule, what is the goal of memcached? Mitigate database stress.
Then according to your own website analysis, to find a large number of traffic changes, we go to see NetEase cloud Music website, see those places memcached can come in handy.


Open the first page to see the middle section is a hot recommendation 1, the recommendation of this place about a few minutes to dozens of minutes are constant, the traffic is very large, casually click
A recommended go in see 2,

Contains some information about the creation of this album, as well as the list of songs contained within it, user reviews, etc.
You can see that the current number of plays in the upper-right corner of the list is 160,000 times, that is, 160,000 read the database (not including some statistical updates of comments and albums), using Memcached, after the first user has requested this page to cache into memcached,
After the user request to go directly memcached, using memcached to let 160,000 read the database down to 1 times!! ,
Statistics on comments and albums can also go memcached, and then synchronize to the database regularly, greatly reducing the amount of database load.

(4)
The usage is simple, just give an example, more function methods please see the official PHP manual
$m = new Memcached ();
$m->addserver (' localhost ', 11211);
$m->flush ();
$m->add ("Key1", "value1");
class People {
function __construct ($name) {
$this->name = $name;
}
function GetName () {
return $this->name;
}
}
$p = new People ("Tom");
$s = serialize ($p);
$m->add ("DD", $s);
Echo unserialize ($m->get ("DD"))->getname ();
echo "<br>". $m->get ("Key1");

(5)

$m = new Memcached ();
$servers = Array (
The third parameter is the weight, the data is randomly inserted into a memcached server, the larger the weight, the greater the probability of being used
Array (' 192.168.150.130 ', 11211, 20),
Array (' 192.168.150.131 ', 11211, 30)
Array (' 192.168.150.133 ', 11211, 50)
);
Three hosts are usually in a computer room, three machines using LAN connectivity, of course, this is not necessary, in principle, as long as three of the machine can exchange the line
$m->addservers ($servers);
$m->flush ();
$m->add ("Key1", "value1");


The other operation is the same as the example above

Memcached+php+mysql+linux Practice

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.