Php reads data from memcache and writes data to mysql in batches. memcachemysql_PHP tutorial

Source: Internet
Author: User
Tags addall
Memcachemysql is a php method for reading data from memcache and writing data into mysql in batches. Php reads data from memcache and then writes data to mysql in batches. memcachemysql describes how php reads data from memcache and then writes data to mysql in batches. Share it with you for your reference. Php reads data from memcache and then writes data to mysql in batches. memcachemysql

This example describes how php reads data from memcache and then writes data to mysql in batches. Share it with you for your reference. The specific analysis is as follows:

Using Memcache can relieve php and database pressure the following code solves the database write bottleneck problem under high load. the most practical problem is that when the ip pv uv is written, the user reaches tens of thousands of visits per minute, to record this data, real-time writing to the database will surely crash.

The following technologies can be used to solve the problem. for example, if a user is registered and a large number of users are registered at the same time, they can be cached and written to the database at one time. the code is as follows:

The code is as follows:

Public function cldata (){
$ Memcache_obj = new Memcache;
$ Memcache_obj-> connect ('127. 0.0.1 ', '123 ');
$ 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 ('cacheddump', $ number, 0 );
$ Line = $ str [$ option];
If (is_array ($ line) & count ($ line)> 0 ){
Foreach ($ line as $ key => $ value ){
$ Keys [] = $ key;
}
}
}
}
}

Dump (count ($ keys); // get the key
If (count ($ keys)> 50) {// number of data entries 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); // log on to write
If (is_array ($ logdata )){
$ This-> addsuidinlogin ($ logdata [0], $ logdata [1], $ logdata [2], 1 );
}*/

/* $ Sdata = unserialize (S ('regadd'. $ ksv); // register the 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 '); // batch write addall
$ Addb-> table ('MJ _ ad_count ')-> addall ($ ress );
Echo M ()-> getLastSql ();
}


Supplement: available tools such as memadmin and memadmin documents.

I hope this article will help you with php programming.

Examples in this article describes how php reads data from memcache and then writes data to mysql in batches. Share it with you for your reference ....

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.