PHP calls memcache to store session

WBOY
Release: 2016-08-08 09:24:07
Original
950 people have browsed it

When php calls memcache to store the session in the following way, remember to set session.auto_start = 0 in

/usr/local/php/etc/php.ini to 0, otherwise calling memcache to store the session will not take effect. of.

The reason is: when php executes the first line, the session will be automatically started, and the default is files, so the session will be saved through the file by default, and the ini_set configured later will be useless.

// Session setting

Method 1:

This method is used in the production environment, there is no problem.

Add an initialization command at the beginning of the line of the php code file.

ini_set("session.save_handler", "memcache");

#If you need to configure multiple memcache addresses, just separate them with commas.

#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();

Method 2:

This method is to write a memcache node, which has been used in the production environment, and the IP addresses of the two memcache configurations have not been verified.

Set directly /usr/local/php/etc/php.ini

session.save_handler = memcache

session.save_path = "tcp://127.0.0.1:11211"

set_session.php

# ################################################ ########

ini_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 accessing set_session.php through the browser, we can telnet 10.12.4.25 11211 to check whether the session value really exists, and it is proved that it is only saved on one of the memcache nodes

get rtv10q183u28kmmtfpi0bd5nq6

# ################################################ #########

Experiment whether the storage is distributed on multiple memcache nodes after configuring multiple memcache nodes

################### ################################################ ########

1. Configure memcache for multiple nodes, and then assign values ​​to them. You will find that they are scattered and stored on multiple nodes

$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 < 20; $i++)
{
$memcache->set($i, $i."hehe", 0, 1000);
}
for ($i = 0; $i < 20; $i++)
{
$val = $memcache->get("$i");
echo "Get $i key1 value: " . $val ."}
?>

Through get_memcache.php you can see that the data is indeed scattered across multiple nodes.

echo '10.12.4.25 11211
';
$memcache = new Memcache;
$memcache->addServer('10.12.4.25', 11211);
#$memcache->addServer ('10.12.4.25', 11213);
for ($i = 0; $i < 20; $i++)
{
$val = $memcache->get("$i");
echo "Get $i key1 value: " . $val ."
n";
}
echo '10.12.4.25 11212
';
$memcache = new Memcache;
$memcache->addServer ('10.12.4.25', 11212);
for ($i = 0; $i < 20; $i++)
{
          $val = $memcache->get("$i");
            echo "Get $i key1 value: " . $val ."
n";
}
echo '10.12.4.25 11213
';
$memcache = new Memcache;
$memcache->addServer('10.12. 4.25', 11213);
for ($i = 0; $i < 20; $i++)
{
          $val = $memcache->get("$i");
                                          using   using                    : " . $val ."
n";
}
?>

is used to clear the memcache value through clean_memcache.php, clear all, and then re-assign and check to see if there is any problem. It was found that the storage was indeed dispersed.

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

When php calls memcache to store the session in the following way, remember to set session.auto_start = 0 in

/usr/local/php/etc/php.ini to 0, otherwise, call memcache to store the session. is ineffective.

//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();

The above introduces how PHP calls memcache to store sessions, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template