Use memcache to synchronize sessions

Source: Internet
Author: User

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

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.