How php reads data from memcache and then writes it to mysql in batches, memcachemysql
The example in this article describes how PHP reads data from memcache and then writes it to mysql in batches. Share it with everyone for your reference. The specific analysis is as follows:
Using Memcache can alleviate the pressure on PHP and database. The following code is to solve the database writing bottleneck problem under high load. The most practical one is: when writing ip pv uv, the user reaches tens of thousands of visits per minute. These should be recorded. Data written to the database in real time will inevitably crash.
The following technology can be used to solve the problem. For example, if a large number of users register at the same time, they can be cached and written to the database at once. The code is as follows:
Copy code The 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 key
If(count($keys)>50){//Number of data to be written
$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 writing
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'));//Batch write addall
$addb->table('mj_ad_count')->addall($ress);
echo M()->getLastSql();
}
Supplement: Tools that can be used include: memadmin and memadmin documentation.
I hope this article will be helpful to everyone’s PHP programming design.
http://www.bkjia.com/PHPjc/934928.html
www.bkjia.com
true
http: //www.bkjia.com/PHPjc/934928.htmlTechArticleHow php reads data from memcache and then writes it to mysql in batches, memcachemysql This article describes how php reads data from memcache How to write data to mysql in batches. Share it with everyone for your reference. ...