PHP read data from memcache and then batch write MySQL method, memcachemysql
This article explains how PHP reads data from memcache and then writes to MySQL in bulk. Share to everyone for your reference. The specific analysis is as follows:
With Memcache can alleviate PHP and database pressure under the code is to solve the high load database write bottleneck problem, encountered the most practical: when the IP PV UV, the user reached tens of thousands of visits per minute, to record these data, real-time write to the database will be rushed to collapse.
With the following techniques can be solved, as well as user registration, the same time off, a large number of user registration, can be cached once written to the database, the code is as follows:
Copy CodeThe code is 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: The tools you can use such as: Memadmin and memadmin documentation.
I hope this article is helpful to everyone's PHP programming.
http://www.bkjia.com/PHPjc/934928.html www.bkjia.com true http://www.bkjia.com/PHPjc/934928.html techarticle PHP read data from memcache and then batch write MySQL method, memcachemysql This article describes the PHP from memcache read data and then batch write MySQL method. Share to everyone for your reference. ...