Yesterday, I watched ghost.ArticleThe idea is good, but it feels too messy,
There was another mistake. Maybe this old man has a tight schedule, so the exam is not comprehensive. Today I have reorganized it here, and I have done some experiments to get it out.
For everyone to learn.
/*
Index. phpProgramPortal, used to construct SQL statements (such as queries and updates)
Config. PHP configuration parameters (memcache, MySQL)
Init. php encapsulate memcached operations (memcache connection, setting, and obtaining)
Mysqli. php closes MySQL operations (MySQL master connection, MySQL slave connection, MySQL master update, MySQL slave query)
*/
Index. php
<? PHP
Require 'init. php'; // load init. php
$ Mem = new memcached; // creates a memcached object. The object is $ mem.
/*
$ Mem-> set ('en _ xx', 'bucuo ',); // test the connection to memcached for debugging.
Echo ($ mem-> get ('en _ xx '));
$ Mem-> set ('cn _ jjyy ', 'wokao );
Echo ($ mem-> get ('cn _ jjyy '));
*/
Require 'mysqli. php ';
$ Sq = new MySQL;
// The following two SQL statements are constructed manually, which can also be used as an interface
$ SQL = "select * from traffic ";
// $ SQL = "insert into traffic (ID, acct_mth, amount) values (14,14, 46 )";
// Make a judgment. If the SQL statement has a select header (Case Insensitive)
If (preg_match ("/^ select/I", $ SQL )){
$ Mdsql = MD5 ($ SQL );
If (! $ Result = $ mem-> get ('cn _'. $ mdsql) {// here we select CN memcached. You can also add data to another memcached based on the preceding conditions.
$ Result = $ SQ-> fetarray ($ SQL); // the query is from MySQL
Foreach ($ result as $ var ){
Echo $ var ['amount'];
}
$ Mem-> set ('cn _ '. $ mdsql, $ result, 0,600); // Add it to the memcached server named CN
} Else {
Foreach ($ result as $ var ){
Echo $ var ['amount']. "<br> ";
}
}
} Else {
$ SQ-> mquery ($ SQL); // The update is primary MySQL
}
?>
Config. php
<? PHP
$ Memcached = array (
'Cn' => array ('192. 192. X. Y', 168 ),
'En' => array ('192. 192. Y. x', 168)
);
$ Mysql = array (
'Master' => array ('x', 'root', '', 'test '),
'Slave _ 1' => array ('y', 'root', '', 'test '),
'Slave _ 2' => array ('Z', 'root', '', 'test') // You can flexibly add multiple slave servers.
);
?>
Init. php
<? PHP
Class memcached
{
Private $ MEM;
Public $ PFLAG = ''; // memcached pconnect tag
Private function memconnect ($ serkey) {// creates a memcached connection, which is easy to understand.
Require 'config. php ';
$ Server = $ memcached; // $ memcached is an array of CN and en.
$ 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 ='') {// you can call this operation to obtain data.
$ 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 );
}
/* This function is used for debugging in the class.
Public Function show ($ messages ){
Echo $ messages;
}
*/
}
?>
Mysqli. php
<? PHP
Class MySQL
{
/* This function is used for debugging.
Public Function show ($ messages ){
Var_dump ($ messages );
}
*/
Private $ mysqlmaster;
Private $ myssqlslave;
Public Function _ construct () {// Constructor
Require 'config. php ';
$ MSG = $ mysql; // $ MySQL is a master, slave_1, and slave_2
$ 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) {// This function is used to obtain the serial number of the slave server, for example, 1, 2.
Session_start (); // start the session
$ _ Session ['sid ']! = 0 | $ _ session ['sid '] = 0; // if the value is not 0, no value assignment is performed. (If the value is not 0 for the first access, a value assignment will be performed)
If ($ _ session ['sid ']> = count ($ mysql)-1) $ _ session ['sid'] = 1; // The above action is prepared for this action
Else $ _ session ['sid '] ++;
$ Key = 'slave _ '. $ _ session ['sid'];
Return new mysqli ($ mysql [$ key] [0], $ mysql [$ key] [1], $ mysql [$ key] [2], $ mysql [$ key] [3]);
}
Public Function mquery ($ SQL) {// MySQL primary insert update
If (! $ This-> mysqlmaster-> query ($ SQL )){
Return false;
}
}
Public Function squery ($ SQL) {// MySQL slave Query
If ($ result = $ this-> mysqlslave-> query ($ SQL )){
Return $ result;
} Else {
Return false;
};
}
Public Function fetarray ($ SQL) {// MySQL query entry
If ($ result = $ this-> squery ($ SQL )){
While ($ ROW = $ result-> fetch_array (mysqli_assoc )){
$ Resultraa [] = $ row;
};
Return $ resultraa;
}
}
}
?>
This is done. I drew the picture. Have you understood it?
This article is from the "bad boy" blog. Be sure to keep this source HTTP