Redis-based procedures for session processing

Source: Internet
Author: User
Tags allkeys redis server
The method of processing session based on Redis

A Redis-based approach to session processing is as follows.

1 
 Php2 classSession_custom {3     Private $redis;//Redis Instances4     Private $prefix= ' Sess_ ';//session_id prefix5     6 //Session starts, the method is executed to connect the Redis server7      Public functionOpen$path,$name) {8         $this->redis =NewRedis ();9         return $this->redis->connect ("127.0.0.1", 6379);Ten     } One      A     //when the session ends, call the method to close the Redis connection -      Public functionClose () { -         $this->redis->close (); the         return true; -     } -      -     //The method is called when the session holds data, and is called after the script finishes or Session_write_close method calls. +      Public functionWrite$session _id,$data) { -         return $this->redis->hmset ($this->prefix.$session _id,Array(' Expires ' = Time(), ' data ' =$data)); +     } A      at     //after you start the session automatically or by calling the Session_Start () function to start the session manually, PHP internally calls the read callback function to get the session data.  -      Public functionRead$session _id) { -         if($this->redis->exists ($this->prefix.$session _id)) { -             return $this->redis->hget ($this->prefix.$session _id, ' Data '); -         } -         return''; in     } -      to     //This callback function is called when the data in the session is cleared, when the Session_destroy () function is called, or when the session_regenerate_id () function is called and the Destroy parameter is set to TRUE.  +      Public functionDestroy$session _id) { -         if($this->redis->exists ($this->prefix.$session _id)) { the             return $this->redis->del ($this->prefix.$session _id) > 0?true:false; *         } $         return true;Panax Notoginseng     } -      the     //garbage collection functions, call cycles are controlled by the session.gc_probability and Session.gc_divisor parameters +      Public functiongc$maxlifetime) { A         $allKeys=$this->redis->keys ("{$this->prefix}* "); the         foreach($allKeys  as $key) { +             if($this->redis->exists ($key) &&$this->redis->hget ($key, ' expires ') +$maxlifetime< Time()) { -                 $this->redis->del ($key); $             } $         } -         return true; -     } the } - Wuyi //invoking a custom session processing method the $handler=NewSession_custom (); - Session_set_save_handler( Wu     Array($handler, ' open '), -     Array($handler, ' close '), About     Array($handler, ' read '), $     Array($handler, ' write '), -     Array($handler, ' Destroy '), -     Array($handler, ' GC ') - ); A  + //the following line of code prevents the use of an object as an unintended behavior that might be thrown when the manager is saved by the session, indicating that the current session data is stored and the current session is closed after the script executes or after exit () is called the register_shutdown_function(' Session_write_close '); -  $ Session_Start(); the  the //you can use the session.

Add:

The session.gc_probability in the php.ini file together with the Session.gc_divisor two configuration options determine the timing of the GC function call. The default values are divided into 1 and 1000, which means that each request has only 1/1000 chance of calling the GC function.

  • Related Article

    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.