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 ....