[Original] PHP development in Multi-memcached and mysql master-slave environments: Code details

Source: Internet
Author: User
Tags pconnect

. Today is the last day in this company. The mood is not good.

Therefore, the general practice of a large station is to use memory as a database (Memcached). And a good backup mechanism for read/write separation (mysql Master/Slave)

In this environment, how can we develop PHP? I am not very talkative. So I should post the code.

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.

<? Php
$Memcached= Array (// useMemcachedMulti-Process Simulation of multipleMemcachedThe server 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.

[Php]
<? Php
Class Memcached
{
Private $ mem;
Public $ pflag = '';//MemcachedPconnect 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 ('MemcachedConnect 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 );
}
}
?>
[/Php]

Simple EncapsulationMemcachedThe detailed time is not long. I'm leaving the company.

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


[Php]
<? Php
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;
}
}
}
?>
[/Php]

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

[Php]
<? Php
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 toMemcachedServer
} Else {
Foreach ($ result as $ var ){
Echo $ var ['pid'];
}
}
?>
[/Php]

This is a program. You can see it later.
The big site is almost implemented in this way. This kind of post seems to have only been me.

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.