[Explain] php development in Multi-memcached and MySQL master-slave environments: Code details

Source: Internet
Author: User
Tags pconnect

From: http://www.pccode.net/info/2010/02/09/20100209-1556.html

The general practice of a large station is to use memory as a database (memcached), and a good backup mechanism for read/write splitting (MySQL Master/Slave)
In this environment, how can we develop PHP?CodeRight.
I just wrote a demo in Linux Vim for debugging. I also hope you can make a brick and write it using PhP5. PhP4 for fear that I am falling behind.
Copy the PHP content to the clipboard. PHP code:

$ memcached = array (// use memcached's multi-process to simulate multiple memcached servers. The cn En is the name of the memory server.
'cn' => array ('2017. 192. 168.254.144', 11211),
'en' => array ('2017. 168.254.144 ', 11212)
);
$ mysql = array (// MySQL Master/Slave my environment is: XP main Linux from MySQL 5 PhP5
'master' => array ('2017. 168.254.213 ', 'root', '1', 'mydz'),
'slave _ 1' => array ('2017. 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 the PHP content to the clipboard. PHP code:

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 );
}
}
?>

memcached operations are 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 gives you more flexibility.
in the memory server name, for example, saving $ arr to the en memory server, I will write $ mem-> set ('en _'. $ ARR); Understand
copy the PHP content to the clipboard. PHP code:

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 the PHP content to the clipboard. PHP code:

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'];
}
}
?>

This is to useProgram. I can see it later.
The big station is almost implemented in this way.

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.