PHP calls Memcache storage session

Source: Internet
Author: User



PHP calls Memcache to store the session in the following way, remember to need to/usr/local/php/etc/php.ini Session.auto_start = 0 is set to 0, Otherwise, calling the Memcache storage session is not effective. The reason is: when PHP executes the first line of time to automatically start the session, and the default is files, so the default is to save the session through the file, and after the configuration of the ini_set is useless.
Session Setting method One: This way the production environment is so used, no problem. Add initialization commands to the beginning of the PHP code file. Ini_set ("Session.save_handler", "Memcache"), #如果需要配置多个memcache地址, separated by a direct comma. #ini_set ("Session.save_path", "tcp://127.0.0.1:11211,tcp://127.0.0.1:11212"); Ini_set ("Session.save_path", "tcp:/ /127.0.0.1:11211 "); Session_Start ();
Mode two: This method writes a memcache node, the production environment is used, and the IP configured for two memcache has not been verified. Direct Settings/usr/local/php/etc/php.inisession.save_handler = Memcache
Session.save_path = "tcp://127.0.0.1:11211"



Set_session.php###########################################################<?phpini_set ("Session.save_ Handler "," Memcache "); Ini_set (" Session.save_path "," tcp://10.12.4.25:11211,tcp://10.12.4.25:11212 ");

Session_Start ();
$_session[' TEST3 '] = time ();
Print $_session[' TEST3 '];
Print session_id ();
?>

After set_session.php is accessed via the browser, we can telnet to 10.12.4.25 11211 to see if the value of the session really exists, and it proves that only one of the memcache nodes is guaranteed to be present on the getRtv10q183u28kmmtfpi0bd5nq6############################################################


Experiment after configuring multiple memcache nodes, is it distributed across multiple memcache nodes ######################################################################  ###### #1. Memcache Multi-node configurations, and then assigning them to them, will find that they are scattered across multiple nodes of the <?php
$memcache = new Memcache;
$memcache->addserver (' 10.12.4.25 ', 11211);
$memcache->addserver (' 10.12.4.25 ', 11212);
$memcache->addserver (' 10.12.4.25 ', 11213);
for ($i = 0; $i < $i + +)
{
$memcache->set ($i, $i. " Hehe ", 0, 1000);
}
for ($i = 0; $i < $i + +)
{
$val = $memcache->get ("$i");
echo "Get $i Key1 Value:". $val. " <br>\n ";
}
?>

With get_memcache.php you can see that the data is really scattered across multiple nodes. <?php
Echo ' 10.12.4.25 11211<br> ';
$memcache = new Memcache;
$memcache->addserver (' 10.12.4.25 ', 11211);
# $memcache->addserver (' 10.12.4.25 ', 11213);
for ($i = 0; $i < $i + +)
{
$val = $memcache->get ("$i");
echo "Get $i Key1 Value:". $val. " <br>\n ";
}
Echo ' 10.12.4.25 11212<br> ';
$memcache = new Memcache;
$memcache->addserver (' 10.12.4.25 ', 11212);
for ($i = 0; $i < $i + +)
{
$val = $memcache->get ("$i");
echo "Get $i Key1 Value:". $val. " <br>\n ";
}
Echo ' 10.12.4.25 11213<br> ';
$memcache = new Memcache;
$memcache->addserver (' 10.12.4.25 ', 11213);
for ($i = 0; $i < $i + +)
{
$val = $memcache->get ("$i");
echo "Get $i Key1 Value:". $val. " <br>\n ";
}


?>

By clean_memcache.php to clear the value of memcache, clear all, and then re-assign and view, check if there is a problem, the discovery is really scattered storage. clean_memcache.php<?php
$memcache = new Memcache;
$memcache->connect (' 10.12.4.25 ', 11211);
$memcache->addserver (' 10.12.4.25 ', 11212);
$memcache->addserver (' 10.12.4.25 ', 11213);
# $memcache->addserver (' 10.12.4.25 ', 11214);


$memcache->flush ();
?>
PHP calls Memcache to store the session in the following way, remember to need to/usr/local/php/etc/php.ini Session.auto_start = 0 is set to 0, Otherwise, calling the Memcache storage session is not effective. Session Settings Ini_set ("Session.save_handler", "Memcache"), #ini_set ("Session.save_path", "tcp://127.0.0.1:11211,tcp ://127.0.0.1:11212 "); Ini_set (" Session.save_path "," tcp://127.0.0.1:11211 "); Session_Start ();

PHP calls Memcache storage session

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.