Alternative method for session storage: redis

WBOY
Release: 2016-07-30 13:31:56
Original
1043 people have browsed it

php uses files to store sessions by default. If the amount of concurrency is large, the efficiency is very low. Redis has very good support for high concurrency, so you can use redis to replace file storage sessions.
Here, we introduce the function and usage of PHP’s session_set_save_handler function. This function defines user-level session saving functions (such as open, close, write, etc.). The prototype is as follows:

<code>bool session_set_save_hanler(callback open,callback close,callback read,callback write,callback destory,callback gc)
</code>
Copy after login

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 example of using redis to store sessions.

Write a session management class sessionManager, the code is as follows:

<code><?php

class SessionManager {
    private $reids;
    private $sessionSavePath;
    private $sessionName;
    private $sessi

    public function __construct() {
        $this->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();
    }
}

?>
</code>
Copy after login

The SessionManager constructor is mainly used to connect to the Redis server, use the session_set_save_handler function to set the session callback function, and call the session_start function to enable the session function. Because the open, close and gc callback functions in this example are not very useful, they return true directly.
In the write callback function, use the session id as the key, store the session data as the value in 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 this data. In the destroy callback function, the session ID is used as the key to delete the corresponding session data from the redis server.

To use, just include the SessionManager class and instantiate a SessionManager object. Create a session_set.php file below. Enter the code

<code><?php
    include("SessionManager.php");
    new SessionManager(); //开启session管理
    $_SESSION['username']='hezikuang';//创建session变量
?>
</code>
Copy after login

and then create a session_get.php file. Enter the following code:

<code><?php
    include("SessionManager.php");
    new SessionManager(); //开启session管理
    echo $_SESSION['username'];
?>
</code>
Copy after login

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

Copyright Statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission.

The above introduces the alternative method of session storage, redis, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template