Php development code in memcached and mysql master-slave environments

Source: Internet
Author: User
Tags pconnect
The general practice of a large site is to use memory as a database (memcached), and a good read/write splitting backup mechanism (mysql master/slave). in such an environment, how can we develop PHP. Memcache

The code is as follows:
$ 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.

The code is as follows:
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.
The 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.

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

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.