How PHP session sessions are written to Redis

Source: Internet
Author: User
Session: In a computer, especially in a network application, it is called "conversation control." So this article will share with you how to save session sessions to Redis, need to hurry to learn it.

PHP session sessions are saved as files by default in the session cache directory of the PHP.ini profile settings, and the file save session is inefficient when each user logs in once to generate a unique session_id file on the server when the user logs on to level hundreds of thousands of , there will be hundreds of thousands of files generated on the server, this time the disk's session Directory IO read and write will be very slow, causing the user to log on when the program will spend a lot of money on the session file read and write. When a large number of users log on with insufficient read and write ability, the resulting file is locked by the process of the first operation session, and other requests are blocked. The request hangs at Session_Start () until the session file is unlocked. This is the time to optimize the slow login problem.

Because the locked session file will not be unlocked until the end of the script execution or the session closes normally, in order to prevent a large number of PHP requests (need to use $_session data) to be locked, you can close the session immediately after writing, thus releasing the lock.

Open session session_start ();//write to $_session[' Is_login ']=1;//write to the session and then close the Write Session_write_close () of the previous conversation file;

PHP session sessions are written to Redis

When Redis data is stored in a computer memory bar, the computer's memory bar is sketched at dozens of times times the speed of the hard disk, so if you write the PHP session to the Redis cache, the speed will be much higher. Configuration method:

Mode one, the PHP script is set to change the way PHP session is written <?phpini_set ("Session.save_handler", "Redis"); Ini_set ("Session.save_path", "tcp://127.0.0.1:6379");//If there is a password Ini_set ("Session.save_path", "tcp://127.0.0.1:6379?") Auth=password ");? >
Modify PHP config file Php.inisession.save_handler = Redis Session.save_path = "tcp://127.0.0.1:6379"//If there is a password Session.save_path = "Tcp://127.0.0.1:6379?auth=password"

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.