Hash and expired recycling of PHP's large number of sessions

Source: Internet
Author: User
Tags file system hash sessions

A large server traffic, because the program needs, the expiration of the session set is 3 hours, resulting in/tmp accumulation of nearly 200,000 of the session file. This leads to a sharp increase in the CPU consumed by the kernel. Because session reading and writing involves a lot of small files of random read and write, and is concentrated in a directory, iowait also increased dramatically.

First consider putting the session into memory

The easiest way to do this is to mount/tmp as a TMPFS file system, which is in-memory

The second step is to store the session in a directory that is not in use

PHP itself supports multilevel hashing of sessions

In PHP.ini, the

; Session.save_path =/tmp

To

Session.save_path = "2;/tmp/session"

Indicates that the session is stored in the/tmp/session folder and is in 2 and hashed columns.

Save exit and restart PHP after step three

Step three, create the session storage folder

PHP does not automatically create these folders, but it provides a few scripts for creating folders in the source file. The following script is also useful


i= "0 1 2 3 4 5 6 7 8 9 a B c D E F"
For the ACM in $I;
Todo
for x in $I;
Todo
Mkdir-p/tmp/session/$ACM/$x;
Done
Done
Chown-r nobody:nobody/tmp/session
Chmod-r 1777/tmp/session

Because/TMP is the memory used, after the server restarts, all the files inside will be lost, so you need to add the above script to the/etc/rc.local and put it before starting PHP

The fourth step, the recovery of the session

Session after Session.gc_maxlifetime will expire, but will not be immediately deleted, long time will cause the/TMP space occupy very large. The specific deletion algorithm is too lazy to study. The following command deletes an expired session, and the expiration time I define here is 3 hours.

Find/tmp/session-amin +180-exec rm-rf {} \;

Put it in Cron and run it 10 minutes.



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.