PHP's Memcache module

Source: Internet
Author: User

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

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.