How does php save the session to memcached? How to save phpsession in a distributed manner

Source: Internet
Author: User
Tags crc32
Session_set_save_handler: the method for saving the session of memcached is on the memcached server. 1) Download memcached.

Session_set_save_handler-independent memcached method for saving session

On the memcached server

1) Download memcached

# Wget http://memcached.googlecode.com/files/memcached-1.4.15.tar.gz

2) because memcached depends on libevent, you need to install the libevent Library first. here, install libevent directly in yum.

# Yum install * libevent *

3) install memcached

#./Configure -- prefix =/usr/local/memcached
# Make
# Make install

4) Start memcached

#/Usr/local/memcached/bin/memcached-d-m 4096-p 11211-u root
-D daemon? -P port-u? User-m memory

On the web server

5) install the php memcache module on the web server.

#/Usr/local/php/bin/pecl install memcache

Enable memcache session handler support? [Yes]: yes (select yes here)

6) add the following content to php. ini:

Extension =/usr/local/php/lib/php/extension/ no-debug-non-zts-20090626/memcache. so

7) modify session. save_handler and session. save_path in php. ini as follows:

Session. save_handler = memcache
Session. save_path = "tcp: // memcached server ip address: 11211"

It can also be used in PHP programs

Ini_set ('session. save_handler ', 'memcache ');
Ini_set ('session. save_path ', 'tcp: // memcached server ip: 8080 ');

Note: This method of saving session with memcached is irrelevant to session_set_save_handler.

After installing memcached

In php. ini

Modify session. save_handler to memcache, and modify save_path to the address and port of memcached.

Session. save_handler = memcache
Session. save_path = tcp: // 127.0.0.1: 11211

Memcache's PECL extension is very powerful and supports failover and distributed storage.

The method is simple. you only need to use commas to separate memcached servers in the parameter list of session. save_path. for example:

Session. save_path = "tcp: // 172.16.8.81: 11211, tcp: // 172.16.8.82: 11211, tcp: // 172.16.8.83: 11211"

The saved sessions are saved to each memcached server after being hashed. the hash algorithm memcache supports two types: crc32 and fnv:

Memcache. hash_function = {crc32, fnv}

The fnv algorithm is rarely mentioned in this document. it is said that its hashes are better than crc32, but after the following small program experiment, I found that the distribution of crc32 hashes is more even.

 addServer('localhost', 11211);$memcache->addServer('localhost', 11212);$memcache->flush();$memcache1->connect('localhost', 11211);$memcache2->connect('localhost', 11212);$fp1 = fopen("mem1.txt", "w");$fp2 = fopen("mem2.txt", "w");for ($i = 0; $i < 1000; $i++){$memcache->set($i, $i, 0, 1000);fwrite($fp1, $memcache1->get($i) . " ");fwrite($fp2, $memcache2->get($i) . " ");}fclose($fp1);fclose($fp2);

Then I tested the session storage.

I started three memcached processes for testing.

 connect('localhost', 11211);$memcache1->flush();$memcache2 = new Memcache;$memcache2->connect('localhost', 11212);$memcache2->flush();$memcache3 = new Memcache;$memcache3->connect('localhost', 11213);$memcache3->flush();$fp1 = fopen('mem1.txt', 'w');$fp2 = fopen('mem2.txt', 'w');$fp3 = fopen('mem3.txt', 'w');for ($i = 0; $i < 1000; $i++){session_start();$ssid = session_id();echo $ssid;session_register("id");$_SESSION["id"] = $ssid;session_write_close();fwrite($fp1, $memcache1->get($ssid) . ' ');fwrite($fp2, $memcache2->get($ssid) . ' ');fwrite($fp3, $memcache3->get($ssid) . ' ');//session_destroy();}fclose($fp1);fclose($fp2);fclose($fp3);

It is strange that memcached2 is generally not selected,

The contents of 1 and 3 are consistent. It may be for failover,

When I close, the content will appear in 2, indicating that memcached2 works normally.

Regardless of whether my hash algorithm uses crc32 or fnv, this phenomenon exists,

Finally, I found a problem with this test program.

Because after session_write_close, the session of the entire program is unique.

That is to say, although the session id returned is the same although it contains the session_destroy call for so many times.

This leads to the consistency of the content in the two files and the absence of content in the other file,

Based on this,

I can only call the script multiple times. The script is modified as follows:

 connect('localhost', 10001);$memcache1->flush();$memcache2 = new Memcache;$memcache2->connect('localhost', 10002);$memcache2->flush();$memcache3 = new Memcache;$memcache3->connect('localhost', 10003);$memcache3->flush();$fp1 = fopen("mem1.txt", "a+");$fp2 = fopen("mem2.txt", "a+");$fp3 = fopen("mem3.txt", "a+");session_start();$ssid = session_id();echo $ssid . "\n";session_register("id");$_SESSION["id"] = $ssid;//session_destroy();session_write_close();fwrite($fp1, $memcache1->get($ssid) . " ");fwrite($fp2, $memcache2->get($ssid) . " ");fwrite($fp3, $memcache3->get($ssid) . " ");session_destroy();fclose($fp1);fclose($fp2);fclose($fp3);

Then run the statement multiple times in shell, and the returned IDs are different.

Open the mem *. txt file,

In the three files, each session is saved in two of them, and the distribution is different.

This proves that memcache is used to save sessions. one is to implement failover, and the other is to perform hash distribution based on the session id.

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.