Copy Code code as follows:
<?php
$memcached = Array (//memcached multi-process analog multiple memcached server CN en for 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 ')//can flexibly add multiple sets from the server
);
?>
Server configuration file: Very convenient to switch from master to slave. When the main changed from can quickly switch to the main. supports multiple from servers.
Copy Code code 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 simple encapsulation of the memcached operation. There is not much time in detail. I'm leaving the company.
On a memcached multiple server. My 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 allocation based on ID.
This can be a little more flexible.
In memory server name for example save $arr This information to en this memory server I write $mem->set (' En_ '. $arr); Get it.
Copy Code code 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 MYSQLI package. That is, read the encapsulation of the operation from the main writer.
Copy Code code 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 main MySQL
$result = $sq->fetarray ("SELECT * from MyBB"); Query is from MySQL
foreach ($result as $var) {
echo $var [' pid '];
}
$mem->set (' Cn_ '. $mdsql, $result); Add to memcached server named CN
}else{
foreach ($result as $var) {
echo $var [' pid '];
}
}
?>