PHP read data from Memcache and then bulk write to MySQL _php tips

Source: Internet
Author: User
Tags addall mysql in

This article is an example of how PHP reads data from memcache and then writes to MySQL in batches. Share to everyone for your reference. The specific analysis is as follows:

Use Memcache to ease PHP and database pressure The following code is to solve the high load database write bottleneck problem, encountered the most practical: Write IP PV UV, the user reached tens of thousands of per minute traffic, to record the data, real-time write to the database must run.

With the following technology can be resolved, as well as user registration, the same time broken, a large number of user registration, can be cached after a one-time write to the database, the code is as follows:

Copy Code code as follows:
Public Function Cldata () {
$memcache _obj = new Memcache;
$memcache _obj->connect (' 127.0.0.1 ', ' 11211 ');
$all _items = $memcache _obj->getextendedstats (' items ');
foreach ($all _items as $option => $vall) {
if (Isset ($all _items[$option] [' items ']) {
$items = $all _items[$option] [' Items '];
foreach ($items as $number => $item) {
$str = $memcache _obj->getextendedstats (' Cachedump ', $number, 0);
$line = $str [$option];
if (Is_array ($line) && count ($line) > 0) {
foreach ($line as $key => $value) {
$keys [] = $key;
}
}
}
}
}

Dump (count ($keys));//Get to Key
if (count ($keys) >50) {//number of data bars to write
$end = 50;
}else{
$end =count ($keys);
}

for ($i =0; $i <= $end; $i + +) {
if (!strstr ($keys [$i], ' datadb ')) continue;
$KSV = Str_replace (' datadb ', ', ', $keys [$i]);

/* $logdata = unserialize (S (' login '. $ksv))//Login Write
if (Is_array ($logdata)) {
$this->addsuidinlogin ($logdata [0], $logdata [1], $logdata [2],1);
}   */

/* $sdata = unserialize (S (' Regadd '. $ksv));//Register Write
if (Is_array ($sdata)) {
$this->baiduad ($sdata [0], $sdata [1], $sdata [2], $sdata [3], $sdata [4],1];
}
*/
$regdata = Unserialize (S (' datadb '. $ksv));
$ress []= $regdata;
S (' datadb '. $KSV, NULL);

}
$ADDB = M ()->db (66,c (' Db_web_ad '))//Bulk Write AddAll
$addb->table (' Mj_ad_count ')->addall ($ress);
Echo M ()->getlastsql ();
}

Add: You can use tools such as: memadmin and memadmin documentation.

I hope this article will help you with your PHP program design.

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.