Php development code in memcached and MySQL master-slave Environments

Source: Internet
Author: User
Tags pconnect

CopyCode The Code is as follows: <? PHP
$ Memcached = array (// use the memcached multi-process to simulate multiple memcached servers. The cn En is the name of the memory server.
'Cn' => array ('192. 168.254.144 ', 192 ),
'En' => array ('192. 168.254.144 ', 192)
);
$ Mysql = array (// MySQL Master/Slave my environment is: XP master Linux from MySQL 5 PhP5
'Master' => array ('192. 168.254.213 ', 'root', '1', 'mydz '),
'Slave _ 1' => array ('192. 168.254.144 ', 'root', '1', 'mydz') // You can flexibly add multiple slave servers.
);
?>

Server Configuration File: It is very convenient to switch between the master and slave nodes. When the master node is changed, the master node can be quickly switched to the master node. Multiple slave servers are supported.

Copy code The Code is as follows: <? PHP
Class memcached
{
Private $ MEM;
Public $ PFLAG = ''; // memcached pconnect tag
Private function memconnect ($ serkey ){
Require 'config. php ';
$ Server = $ memcached;
$ This-> mem = new memcache;
$ Link =! $ This-> PFLAG? 'Connect ': 'pconnect ';
$ This-> mem-> $ Link ($ server [$ serkey] [0], $ server [$ serkey] [1]) or $ this-> errordie ('memcached connect error ');
}
Public Function set ($ ser_key, $ values, $ flag = '', $ expire = ''){
$ This-> memconnect ($ this-> tag ($ ser_key ));
If ($ this-> mem-> set ($ ser_key, $ values, $ flag, $ expire) return true;
Else return false;
}
Public Function get ($ ser_key ){
$ This-> memconnect ($ this-> tag ($ ser_key ));
If ($ Var = $ this-> mem-> get ($ ser_key) return $ var;
Else return false;
}
Private function tag ($ ser_key ){
$ Tag = explode ('_', $ ser_key );
Return $ tag [0];
}
Private function errordie ($ errmsg ){
Die ($ errmsg );
}
}
?>

The memcached operation is simply encapsulated. The detailed time is not long. I am leaving the company.
On Multiple memcached servers. my implementation idea is as follows: when I add information to the memory server. I chose to manually add the settings to that server. instead of automatically assigned by ID.
This makes it more flexible.
Using the name of the memory server as the representation, for example, saving the $ arr information to the en memory server, I will write $ mem-> set ('en _ '. $ ARR) in this way. I understand.Copy codeThe Code is as follows: Class MySQL
{
Private $ mysqlmaster;
Private $ myssqlslave;
Private Static $ auid = 0;
Public Function _ construct (){
Require 'config. php ';
$ MSG = $ mysql;

$ This-> mysqlmaster = new mysqli ($ MSG ['master'] [0], $ MSG ['master'] [1], $ MSG ['master'] [2], $ MSG ['master'] [3]); // master MySQL
$ This-> mysqlslave = $ this-> autotranscat ($ MSG); // slave MySQL
If (mysqli_connect_errno ()){
Printf ("Connect failed: % s \ n", mysqli_connect_error ());
Exit ();
}
If (! $ This-> mysqlmaster-> set_charset ("Latin1 ")&&! $ This-> mysqlslave-> set_charset ("Latin1 ")){
Exit ("set charset error ");
}
}
Private function autotranscat ($ mysql ){
Session_start ();
$ _ Session ['sid ']! = 0 | $ _ session ['sid '] = 0;
If ($ _ session ['sid ']> = count ($ mysql)-1) $ _ session ['sid'] = 1;
Else $ _ session ['sid '] ++;
$ Key = 'slave _ '. $ _ session ['sid'];
Echo ($ _ session ['sid ']);
Return new mysqli ($ mysql [$ key] [0], $ mysql [$ key] [1], $ mysql [$ key] [2], $ mysql [$ key] [3]);
}
Public Function mquery ($ SQL) {// insert update
If (! $ This-> mysqlmaster-> query ($ SQL )){
Return false;
}
}
Public Function squery ($ SQL ){
If ($ result = $ this-> mysqlslave-> query ($ SQL )){
Return $ result;
} Else {
Return false;
};
}
Public Function fetarray ($ SQL ){
If ($ result = $ this-> squery ($ SQL )){
While ($ ROW = $ result-> fetch_array (mysqli_assoc )){
$ Resultraa [] = $ row;
};
Return $ resultraa;
}
}
}
?>

This is the encapsulation of mysqli, that is, the encapsulation of the read Master operation.

Copy code The Code is as follows: require 'init. php ';
$ Mem = new memcached;
/* $ Mem-> set ('en _ xx', 'bucuo ');
Echo ($ mem-> get ('en _ xx '));
$ Mem-> set ('cn _ jjyy ', 'wokao ');
Echo ($ mem-> get ('cn _ jjyy '));
*/
$ Sq = new MySQL;
$ SQL = "insert into mybb (PID) values (200 )";
$ Mdsql = MD5 ($ SQL );
If (! $ Result = $ mem-> get ('cn _ '. $ mdsql )){
$ SQ-> mquery ("insert into mybb (PID) values (200)"); // insert to primary MySQL
$ Result = $ SQ-> fetarray ("select * From mybb"); // the query is from MySQL
Foreach ($ result as $ var ){
Echo $ var ['pid'];
}
$ Mem-> set ('cn _ '. $ mdsql, $ result); // Add it to the memcached server named CN.
} Else {
Foreach ($ result as $ var ){
Echo $ var ['pid'];
}
}
?>
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.