Alternate method for session storage Redis

Source: Internet
Author: User
Tags session id

PHP uses the file storage session by default, which is very inefficient if the concurrency is large. Redis's support for high concurrency is very good, so you can store the session using Redis alternative files.
Here, we introduce the session_set_save_handler function of PHP and how to use it. This function defines a user-level session save function (such as open, close, write, etc.). 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 of the php.ini configuration file to user, otherwise session_set_save_handle will not take effect.

The following is an instance of the session using Redis storage.

Write a session management class SessionManager with the following code:

<?phpclass SessionManager {private $reids;    Private $sessionSavePath;    Private $sessionName;    Private $sessionExpireTime = 300;        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, $th        Is->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 primarily used to connect to a Redis server, use the Session_set_save_handler function to set the session callback function, and invoke the Session_Start function to open the session function. Because the open, close, and GC callback functions in this example do not work very large, they return true directly.
In the write callback function, with session ID as key, the session data is stored as value to the Redis server, setting the session expiration time is 300 seconds. In the read callback function, the session ID is used as key to read the data from the Redis server and return this data. When the destroy callback function is heavy, the session ID is used as key to remove the corresponding session data from the Redis server.

When used, simply include the SessionManager class and instantiate a SessionManager object. Create a session_set.php file below. Input code

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

Then create a session_get.php file and enter the following code:

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

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

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Alternate method for session storage Redis

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.