PHP Technology session Hash and expiration Recycling _php tutorial

Source: Internet
Author: User
Session, in a computer, especially in a network application, is called a "session."

A server traffic is relatively large, because the program needs, the session expiration time is set to 3 hours, resulting in/TMP piled up nearly 200,000 of the session file. This leads to a sharp increase in CPU consumption by the kernel. Because the session read and write involves a lot of small files random read and write, and is concentrated in a directory, iowait also sharply increased.

Consider putting the session into memory first

The simplest way is to mount/tmp as a TMPFS file system, in memory

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

PHP itself supports multi-level hashing of session

In PHP.ini, the

 
  
  
  1. Session.save_path =/tmp;

Switch

 
  
  
  1. "2;/tmp/session"

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

Save exit, wait until the end of the third step to restart PHP

The third step is to create the session storage folder

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

 
  
  
  1. i= "0 1 2 3 4 5 6 7 8 9 a B c d E F"
  2. for ACM in /span> $I ;
  3. do
  4. for x in $I ;
  5. do
  6. mkdir -p/tmp/session/ $ACM / $x ;
  7. done;
  8. done
  9. chown -R nobody:nobody/tmp /session
  10. chmod -R 1777/tmp/session

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

The fourth step, the recycling session

Session after Session.gc_maxlifetime will expire, but will not be deleted immediately after a long time will cause/TMP space occupies a lot. The specific deletion algorithm is lazy to study. The following command can delete the expired session, I define the expiration time is 3 hours.

 
  
  
  1. find/tmp/session-amin +180- exec RM-RF {};

Put it in cron, execute it in 10 minutes, and do it.

This article has four steps to introduce the hash of the session and the expiration of the collection, I hope it helps you.


http://www.bkjia.com/PHPjc/445794.html www.bkjia.com true http://www.bkjia.com/PHPjc/445794.html techarticle session, in a computer, especially in a network application, is called a conversation. A server traffic is relatively large, because the program needs, the session expiration time is set to 3 hours, resulting in ...

  • 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.