Phpmemcachedmysql detailed instance development _ PHP Tutorial

Source: Internet
Author: User
Tags pconnect
A detailed example of phpmemcachedmysql development. Php Tutorial memcachedmysql tutorial development detailed instance Memcached working method in the following section, the reader is better to prepare a memcached source code. Memcached is a traditional network service process php Tutorial memcached mysql tutorial detailed instance development

How Memcached works

In the following sections, you are advised to prepare the source code of memcached.

Memcached is a traditional network service program. If the-d parameter is used during startup, it will be executed as a daemon process. Daemon. c is used to create daemon. this program has only one daemon function. This function is very simple (unless otherwise stated, the code is subject to 1.2.1 ):

CODE:
# Include
# Include
# Include

Int
Daemon (nochdir, noclose)
Int nochdir, noclose;
{
Int fd;

Switch (fork ()){
Case-1:
Return (-1 );
Case 0:
Break;
Default:
_ Exit (0 );
}

If (setsid () =-1)
Return (-1 );

If (! Nochdir)
(Void) chdir ("/");

If (! Noclose & (fd = open ("/dev/null", O_RDWR, 0 ))! =-1 ){
(Void) dup2 (fd, STDIN_FILENO );
(Void) dup2 (fd, STDOUT_FILENO );
(Void) dup2 (fd, STDERR_FILENO );
If (fd> STDERR_FILENO)
(Void) close (fd );
}
Return (0 );
}

After this function fork the entire process, the parent process will exit, and then relocates STDIN, STDOUT, and STDERR to the empty device, and daemon will be established successfully.

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 );
}
}
?>


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: % sn", 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;
}
}
}
?>

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

$ 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.
);
?>

The starting process of Memcached itself is as follows in the main function of memcached. c:

1. call settings_init () to set initialization parameters.
2. read the parameter from the startup command to set the setting value.
3. set the LIMIT parameter.
4. start network socket listening (if a non-socketpath exists) (UDP is supported after 1.2)
5. check the user identity (Memcached does not allow root startup)
6. if socketpath exists, enable the UNIX local connection (Sock pipeline)
7. if the daemon is started in the-d mode, create a daemon process (call the daemon function above)
8. initialize item, event, status information, hash, connection, and slab.
9. if managed takes effect in the settings, create a bucket array.
10. check whether the memory page needs to be locked.
11. initialize signals, connect and delete queues
12. if the daemon method is used, process the process ID
13. start event and start process. The main function enters the loop.

In the daemon mode, stderr has been redirected to a black hole, so no visible error information is reported during execution.

The main cyclic function of memcached. c is drive_machine. the input parameter is the structure pointer to the current connection. The action is determined based on the state of the state member.

Memcached uses a set of custom protocols for data exchange, its protocol documentation can refer to: http://code.sixapart.com/svn/memcached/trunk/server/doc/protocol.txt

In API, the line feed symbol is rn.


Prepare memcached mysql tutorial to develop a detailed instance of Memcached. in the following section, you are advised to prepare the source code of memcached. Memcached is a traditional network service...

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.