The method of processing session based on Redis

Source: Internet
Author: User
Tags allkeys redis server
A Redis-based approach to session processing is as follows.

1 
 Php2classSession_custom {3Private$redis;//Redis Instances4Private$prefix= ' Sess_ ';//session_id prefix56//Session starts, the method is executed to connect the Redis server7 PublicfunctionOpen$path,$name) {8$this->redis =NewRedis ();9return$this->redis->connect ("127.0.0.1", 6379);Ten    } One A//when the session ends, call the method to close the Redis connection - PublicfunctionClose () { -$this->redis->close (); thereturntrue; -    } - -//The method is called when the session holds data, and is called after the script finishes or Session_write_close method calls. + PublicfunctionWrite$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.  - PublicfunctionRead$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.  + PublicfunctionDestroy$session _id) { -if($this->redis->exists ($this->prefix.$session _id)) { thereturn$this->redis->del ($this->prefix.$session _id) > 0?true:false; *        } $returntrue;Panax Notoginseng    } - the//garbage collection functions, call cycles are controlled by the session.gc_probability and Session.gc_divisor parameters + Publicfunctiongc$maxlifetime) { A$allKeys=$this->redis->keys ("{$this->prefix}* "); theforeach($allKeys as$key) { +if($this->redis->exists ($key) &&$this->redis->hget ($key, ' expires ') +$maxlifetime< Time()) { -$this->redis->del ($key); $            } $        } -returntrue; -    } the} -Wuyi//invoking a custom session processing method the$handler=NewSession_custom (); -Session_set_save_handler( WuArray($handler, ' open '), -Array($handler, ' close '), AboutArray($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 theregister_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.

The above describes the processing session based on the Redis method, including the content, I hope the PHP tutorial interested in a friend helpful.

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