PHP's Memcache module:
: Http://pecl.php.net/package/memcache
Download and configure the Memcache module that generates PHP
wget http://pecl.php.net/get/memcache-3.0.8.tgz
Yum install-y M4 autoconf
TAR-ZXVF memcache-3.0.8.tgz
CD memcache-3.0.8
/usr/local/php/bin/phpize //generate the. config file
./configure--with-php-config=/usr/local/php/bin/php-config
make && make install
mkdir/usr/local/php/ext Create a unified storage extension directory
Open php.ini
If you do not have php.ini {Cp/usr/local/src/php-5.3.27/php.ini-production/usr/local/php/etc/php.ini}
Add: Extension_dir = "/usr/local/php/ext" defines a unified storage directory.
Cp/usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/memcache.so/usr/local/php/ext Copy the module into the directory.
Then edit php.ini at the end to add a line Extension = memcache.so
Check the/usr/local/php/bin/php-m |grep memcache to see if the module is already loaded.
With a PHP script:
<?php
//Connection Memcache Memcache IP address can be changed
$mem = new Memcache;
$mem->connect ("192.168.47.26", 11211);
//Save Data
$mem->set (' key1 ', ' This is first value ', 0,);
$val = $mem->get (' Key1 ');
echo "Get key1 value:". $val. " <br> ";
//Replace data
$mem->replace (' key1 ', ' This is replace value ', 0;
$val = $mem->get (' Key1 ');
echo "Get key1 value:". $val. "<br>";
//Save array Data
$arr = Array (' AAA ', ' BBB ', ' CCC ', ' ddd ');
$mem->set (' Key2 ', $arr, 0,);
$val 2 = $mem->get (' Key2 ');
echo "Get Key2 value:";
Print_r ($val 2);
echo "<br>";
//Delete data
$mem->delete (' Key1 ');
$val = $mem->get (' Key1 ');
echo "Get key1 value:". $val. "<br>";
//Clear All data
$mem->flush ();
$val 2 = $mem->get (' Key2 ');
echo "Get Key2 value:";
Print_r ($val 2);
echo "<br>";
//Close connection
$mem->close ();
?>
To implement session sharing :
To configure the connection in php.ini:
Add:
Session.save_handler = "Memcache"
Session.save_path = "tcp://192.168.47.26:11211"
Configure connection in httpd.conf "LAMP": Apache
Virtual Host Configuration add:
Php_value Session.save_handler "Memcache"
Php_value Session.save_path "tcp://192.168.47.26:11211"
Configure connection in php-fpm.conf "LNMP": Nginx
Add the corresponding pool inside:
Php_value[session.save_handler] = Memcache
Php_value[session.save_path] = "tcp://192.168.47.26:11211"
PHP's Memcache module