Alternative to session storage redis

Source: Internet
Author: User
: This article mainly introduces the alternative method of session storage redis. if you are interested in the PHP Tutorial, please refer to it. Php uses the file storage session by default. if the concurrency is large, the efficiency is very low. Redis provides excellent support for high concurrency. Therefore, you can use redis to replace the file storage session.
Here, we will introduce session_set_save_handlerFunctions and usage methods. This function defines user-level session saving functions (such as opening, closing, and writing ). The prototype is as follows:

bool session_set_save_hanler(callback open,callback close,callback read,callback write,callback destory,callback gc)

Before using this function, set the session. save_handler option in the php. ini configuration file to user. otherwise, session_set_save_handle will not take effect.

The following is an instance that uses redis to store sessions.

Compile a session Manager. the code is as follows:


  reids=new Redis();        $this->reids->connect('127.0.0.1',6379);        $retval=session_set_save_handler(            array($this,"open"),            array($this,"close"),            array($this,"read"),            array($this,"write"),            array($this,"destroy"),            array($this,"gc")        );}public function open($path,$name) {    return true;}public function close() {    return true;}public function read($id) {    $vale=$this->reids->get($id);    if($vale) {        return $vale;    }else {        return '';    }}public function write($id,$data) {    if($this->reids->set($id,$data)) {        $this->reids->expire($id,$this->sessionExpireTime);        return true;    }    return false;}public function destroy($id) {    if($this->reids->delete($id)) {        return true;    }    return false;}public function gc($maxlifetime) {    return true;}    public function __destruct() {        session_write_close();    }}?>

The SessionManager constructor is used to connect to the Redis server. it uses the session_set_save_handler function to set the session callback function and calls the session_start function to enable the session function. In this example, the open, close, and gc callback functions are not very effective, so true is returned directly.
In the write callback function, the session id is used as the key to store the session data as the value to the redis server and set the session expiration time to 300 seconds. In the read callback function, the session ID is used as the key to read data from the redis server and return the data. In the destroy callback function, session ID is used as the key to delete the corresponding session data from the redis server.

In use, you only need to include the SessionManager class and then instantiate a SessionManager object. Create a session_set.php file. Enter code


  

Create another session_get.php file and enter the following code:


  

You can access session_get.php to see if it is successful.

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

The above describes the alternative method of session storage redis, including the content, hope to be helpful to friends interested in PHP tutorials.

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.