It is good to use memcache to synchronize sessions. Of course, you can also use redis to save sessions. You can enable PHP and store sessions to redis cache, the following describes how to use memcache to synchronize sessions in a Web Cluster:
1. simulate a Web Cluster
I started two memcached processes, simulating two servers respectively.
/Usr/local/bin/memcached-D-M 20-u zhangy-P 12000-P./memcached. PID
/Usr/local/bin/memcached-D-M 20-u zhangy-P 13000-P./MEM. PID
2. Modify the PHP configuration
VI/usr/local/PHP/lib/PHP. ini
Session. save_handler = "memcache"
Memcache. hash_strategy = "consistent"
Session. save_path = "TCP: // FIG: 13000? Weight = 10, TCP: // 127.0.0.1: 12000"
Note: In the first line, the session storage method is memcache; in the second line, the hash algorithm of memcache is consistent; in the third line, the session storage status;
3. Restart Apache
View phpinfo
Session
Session support |
Enabled |
Registered save handlers |
Files user SQLite memcache |
Registered serializer handlers |
PHP php_binary |
Next is:
Session. save_path |
TCP: // 127.0.0.1: 13000, TCP: // 127.0.0.1: 12000 |
TCP: // 127.0.0.1: 13000, TCP: // 127.0.0.1: 12000 |
4. perform a simple test as follows:
A) Prepare the file session. php
?
12345 |
<?php session_start(); $_SESSION [ ‘username‘ ] = "abcabc" ; echo session_id(); ?> |
B) display the session Content File
?
12345678 |
<?php $mem = new Memcache; $mem ->addServer( "127.0.0.1" ,12000) or die ( "Could not add server 12000" ); $mem ->addServer( "127.0.0.1" ,13000) or die ( "Could not add server 13000" ); $val = $mem ->get( ‘qp0mrob2ovcqle3u4lbr4obsa5‘ ); // Echo session_id (); obtains the session ID. echo $val ; ?> |
Use memcache to synchronize sessions