I. Introduction of Memcache
Memcache is a danga.com project, the first to serve LiveJournal, many people around the world use this cache project to build their own heavy load of the site, to share the pressure of the database.
Memcache Official website: http://memcached.org/
Second, the installation of Memcache
1, download the source file (currently the latest stable version is memcached-1.4.5.tar.gz)
wget http://memcached.googlecode.com/files/memcached-1.4.5.tar.gz
Since Memcache used libevent this library for socket processing, you also need to install libevent,
Libevent's official website is http://www.monkey.org/~provos/libevent/, the latest stable version is: 1.4.14b
wget http://www.monkey.org/~provos/libevent-1.4.14b-stable.tar.gz
2, installation Memcache
1), install Libevent
# tar XZVF libevent-1.4.14b-stable.tar.gz
# CD Libevent-1.4.14b-stable
#./configure--PREFIX=/USR
# Make && make install
# CD:
# Ls-al/usr/lib | grep libevent (see if the installation was successful)
2), install Memcache
# tar XZVF memcached-1.4.5.tar.gz
# CD memcached-1.4.5
#./configure--WITH-LIBEVENT=/USR
# Make && make install
# ls-al/usr/local/bin/mem* (see if memcached executable file is generated)
#备注: If the 64-bit system may error, the default library file will only be installed under/usr/lib
3. Start Memcache
# See command Options help with/usr/local/bin/memcached–h
[Email protected] memcached-1.4.5]#/usr/local/bin/memcached-h
Memcached 1.4.5
-P <num> TCP port number to listen on (default:11211)
-U <num> UDP port number to listen on (default:11211, 0 is off)
-S <file> UNIX socket path to listen on (disables network support)
-A <mask> access mask for UNIX sockets, in octal (default:0700)
-L <ip_addr> interface to listen in (Default:inaddr_any, all addresses)
-D Run as a daemon
-R Maximize Core file limit
-U <username> assume identity of <username> (only if run as root)
-M <num> max memory to use for items in megabytes (default:64 MB)
-M return error on memory exhausted (rather than removing items)
-C <num> Max simultaneous connections (default:1024)
#启动命令如下
#/usr/local/bin/memcached-d-M 10-u root-l 172.28.5.2-p 12000-c 1024–p/tmp/memcached.pid
# # #相关选项说明
-d means to start a daemon
-M is the memory allocated to memcached use
-U user running memcached
-L is the IP of memcached monitoring
-P is the memcached listening port
-C memcache Maximum number of concurrent connections running
-P is a PID file that sets Memcache
Third, the installation of PHP memcache extension
1. Installation of Windows platform Memcache extension
Download the appropriate version of the Php_memcache.dll file, and then put the file in the D:\AppServ\php5\ext directory (the path is set according to the location of the PHP installation), and then change the settings of the corresponding php.ini to create <?php Phpinfo ();? > PHP file, Access this file to see if the Memcache module works.
2. Installation of Linux platform Memcache extension
wget http://blog.s135.com/soft/linux/nginx_php/memcache/memcache-2.2.5.tgz
Tar zxvf memcache-2.2.5.tgz
CD memcache-2.2.5/
/usr/local/php/bin/phpize
./configure--with-php-config=/usr/local/php/bin/php-config
Make
Make install
Cd.. /
Modify the php.ini file
Find Extension_dir = "./" In/usr/local/php/etc/php.ini = "modified to Extension_dir ="/usr/local/php/lib/php/extensions/ no-debug-non-zts-20060613/"
and add a row after this line, and then save:
Extension = "memcache.so"
Create PHP file aaa.php with the following content:
<?php
Phpinfo ();
?>
Save and then visit to see if you can display the information of the Memcache module!
Iv. Use of Memcache
The use of the part is reproduced, the original address: Http://blog.csdn.net/heiyeshuwu
[ interface Introduction ]
The server side and the client are all installed, so let's test the results below. The Memcache client contains two sets of interfaces, one for the process-oriented interface, and one for the object-oriented interface, which can be referenced in the PHP manual "LXXV". Memcache Functions "this chapter. We use an object-oriented approach for simplicity and ease of maintenance and writing code. Memcache Common object-oriented interfaces include:
Memcache::connect--Open a connection to Memcache
Memcache::p Connect--open a long connection to Memcache
Memcache::close--Close a Memcache connection
Memcache::set--Save data to the Memcache server
Memcache::get--Extract a data stored on the Memcache server
Memcache::replace--Replace an item that already exists on the Memcache server (functionally similar to memcache::set)
Memcache::d elete--delete a saved item from the Memcache server
Memcache::flush--Refreshes all items saved on the Memcache server (similar to deleting all saved items)
Memcache::getstats--Get the status of the current Memcache server running
[ test Code ]
Now let's start with a test code:
<?php
Connection
$mem = new Memcache;
$mem->connect ("192.168.0.200", 12000);
Save data
$mem->set (' key1 ', ' This is first value ', 0, 60);
$val = $mem->get (' Key1 ');
echo "Get Key1 value:". $val. "
";
Replace data
$mem->replace (' key1 ', ' This is replace value ', 0, 60);
$val = $mem->get (' Key1 ');
echo "Get Key1 value:". $val. "
";
Save Array
$arr = Array (' AAA ', ' BBB ', ' CCC ', ' ddd ');
$mem->set (' Key2 ', $arr, 0, 60);
$val 2 = $mem->get (' Key2 ');
echo "Get Key2 value:";
Print_r ($val 2);
echo "
";
Delete data
$mem->delete (' Key1 ');
$val = $mem->get (' Key1 ');
echo "Get Key1 value:". $val. "
";
Clear All data
$mem->flush ();
$val 2 = $mem->get (' Key2 ');
echo "Get Key2 value:";
Print_r ($val 2);
echo "
";
Close connection
$mem->close ();
Add more than one memcached server
$b = new Memcache ();
$b->addserver ("10.55.38.18", 11271);
$b->addserver ("10.55.38.18", 11272);
$b->addserver ("10.55.38.18", 11273);
$arrHosts = Array (' 10.10.10.11 ' = ' 11211 ', ' 10.10.10.12 ' = ' 11211 ');
$MC = new Memcache ();
Echo Serialize ($MC). " \ n ";
foreach ($arrHosts as $host = = $port) {
Echo ' Start '. $host. " \ n ";
$MC->connect ($host, $port);
Echo Serialize ($MC). " \ n ";
$stats = $MC->getstats ();
echo $stats [' pid ']. \ n ";
}
?>
If normal, the browser will output:
Get key1 Value:this is first value
Get key1 Value:this is replace value
Get key2 Value:array ([0] = AAA [1] = BBB [2] = CCC [3] = = DDD)
Get Key1 Value:
Get Key2 Value:
Basic instructions Our Memcache installation is successful, we will analyze the following procedure.
[ program Analysis ]
Initializes an Memcache object:
$mem = new Memcache;
Connect to our Memcache server, the first parameter is the IP address of the server, the host name, and the second parameter is the open port of Memcache:
$mem->connect ("192.168.0.200", 12000);
Save a data to the Memcache server, the first parameter is the data key, used to locate a data, the second parameter is to save the data content, here is a string, the third parameter is a marker, generally set to 0 or memcache_compressed on the line, The fourth parameter is the validity period of the data, that is, the data in this time is valid, if the past this time, then the memcache server side will be purged of this data, the unit is seconds, if set to 0, it is always valid, we set up here 60, is a minute effective time:
$mem->set (' key1 ', ' This is first value ', 0, 60);
Get a piece of data from the Memcache server, it has only one parameter, is the key that needs to get the data, we here is the key1 that the previous step sets, now obtains this data after output:
$val = $mem->get (' Key1 ');
echo "Get Key1 value:". $val;
Instead of replacing the value of the above key1 with the Replace method, the Replace method parameter is the same as set, but the first parameter, Key1, must be the key to replace the data content, and the final output is:
$mem->replace (' key1 ', ' This is replace value ', 0, 60);
$val = $mem->get (' Key1 ');
echo "Get Key1 value:". $val;
Similarly, memcache can also save an array, the following is an array saved on memcache, and then get back and output
$arr = Array (' AAA ', ' BBB ', ' CCC ', ' ddd ');
$mem->set (' Key2 ', $arr, 0, 60);
$val 2 = $mem->get (' Key2 ');
Print_r ($val 2);
Now delete a data, using the Delte interface, the parameter is a key, and then can be the Memcache server this key data deleted, the final output when no results
$mem->delete (' Key1 ');
$val = $mem->get (' Key1 ');
echo "Get Key1 value:". $val. "
";
Finally, we put all the data stored on the Memcache server erased, we will find that the data is not, the final output key2 data is empty, and finally close the connection
$mem->flush ();
$val 2 = $mem->get (' Key2 ');
echo "Get Key2 value:";
Print_r ($val 2);
echo "
";
Deployment and use of memcache (RPM)