[Original] multi-memcached and MySQL in the master-Slave environment PHP Development: code detailed _php Tutorial

Source: Internet
Author: User
Tags mysql in pconnect
It's 4 o'clock. Today is the last day in this company. The mood is not very good.

So write down something to vent. General stations usually take the memory as a database to use ( memcached). And a good read-write separation backup mechanism (MySQL master and slave)

How do we develop PHP in such an environment? I'm not very good at speaking. So stick to the code.

Just in the Linux VIM to write a demo debug pass. Also want everyone to shoot bricks, use PHP5 write. PHP4 wrote out that I was afraid that I was behind.

$ memcached= Array (//With memcachedMulti-process simulation of multiple units memcachedServer CN en is the memory server name
' CN ' =>array (' 192.168.254.144 ', 11211),
' En ' =>array (' 192.168.254.144 ', 11212)
);
$mysql = Array (//MySQL master-slave My environment is: XP main Linux from MySQL 5 php5
' Master ' =>array (' 192.168.254.213 ', ' root ', ' 1 ', ' Mydz '),
' Slave_1 ' =>array (' 192.168.254.144 ', ' root ', ' 1 ', ' mydz ')//flexibility to add multiple slave servers
);
?>

Server configuration file: Very convenient to switch master and slave. When the master changed from can quickly switch to main. Multiple slave servers are supported.

[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]

It's a simple package. memcachedThe operation. There is not much time for detail. I'm leaving the company.

In memcachedon multiple servers. My idea for implementation is this: When adding information to a memory server. I chose to add the manual settings to that server. Instead of the traditional automatic assignment by ID.
This gives you a more flexible point.

The memory server name represents such as the storage $arr this information to en this memory server I wrote $mem->set (' En_ '. $arr); Got it.


[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 MYSQLI package. That is, the encapsulation of the operation that reads from the write master.

[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 main MySQL
$result = $sq->fetarray ("SELECT * from MyBB"); Query is from MySQL
foreach ($result as $var) {
echo $var [' pid '];
}
$mem->set (' Cn_ ' $mdsql, $result); Added to a named CN memcachedServer
}else{
foreach ($result as $var) {
echo $var [' pid '];
}
}
?>
[/php]

This is the use of the program. Look at it, you probably understand.
The station is almost the way it is done. This kind of post seems to me only. Caught dead.

http://www.bkjia.com/PHPjc/735128.html www.bkjia.com true http://www.bkjia.com/PHPjc/735128.html techarticle It 's 4. Today is the last day in this company. The mood is not very good. So write down something to vent. Generally the main station is to hold the memory when the database is used (memcached). And very well read ...

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