Details about using redis to store session instances in php

Source: Internet
Author: User
Tags hash redis sessions

Phpinfo, you can see the session storage, you can use files user memcache redis, the advantage of using the database to store sessions is that compared with the file storage, the speed is faster and the performance is better in a large number of users, in addition, for a distributed system, you must use a database to store sessions. Here we will summarize the two methods for storing session in redis.
We should have used mysql to store Sessions. The principle of the session lifecycle is actually determined by the two parameters session. gcprobability, session. gcdivisor and the maximum life time. Each php request has a certain probability of triggering the session detection mechanism. Session_setsavehandler can be used to redefine session behavior. There are two ways to implement redis storage session

Code Example 1:

The code is as follows: Copy code

<? Php
Ini_set ('session. Gc_maxlifetime', 3600 );
Ini_set ("session. save_handler", "redis ");
Ini_set ("session. save_path", "tcp: // fig: 6379? Auth = authpwd ");
Session_start ();
// $ _ SESSION ['session'] = 'This is session content! ';
Echo $ _ SESSION ['session '];
Echo session_id (). '<br/> ';
 
$ Redis = new redis ();
$ Redis-> connect ('2017. 0.0.1 ', 127 );
// Redis uses session_id as the key and stores it as a string
Echo $ redis-> get ('phpredis _ SESSION: '. session_id ());
?>

Code Example 2:

The code is as follows: Copy code
<? Php
 
Class RedisSessionHandler {
Public $ ttl = 1800; // 30 minutes default
Protected $ db;
Protected $ prefix;
Public function _ construct ($ db, $ prefix = 'phpsessid1: ', $ time = 1800 ){
$ This-> db = $ db;
$ This-> prefix = $ prefix;
$ This-> ttl = $ time;
    }
Function _ open ()
    {
//
    }
 
Function _ close ()
    {
$ This-> db = null;
Unset ($ this-> db );
    }
 
Function _ read ($ id)
    {
$ Id = $ this-> prefix. $ id;
$ SessData = $ this-> db-> get ($ id );
$ This-> db-> expire ($ id, $ this-> ttl );
Return $ sessData;
    }
 
Function _ write ($ id, $ data)
    {
$ Id = $ this-> prefix. $ id;
$ This-> db-> set ($ id, $ data );
$ This-> db-> expire ($ id, $ this-> ttl );
    }
 
Function _ destroy ($ id)
    {
$ This-> db-> del ($ this-> prefix. $ id );
    }
 
Function _ clean ($ max)
    {
//
    }
}
$ Redis = new Redis ();
$ Redis-> connect ('2017. 0.0.1 ', 127 );
$ Redis-> select (1 );
$ SESSION_ID_PREFIX = 'rsid :';
$ SESSION_MAX_TIME = 1440;
Ini_set ('session. gc_maxlifetime ', $ SESSION_MAX_TIME );
$ SessHandler = new RedisSessionHandler ($ redis, $ SESSION_ID_PREFIX, $ SESSION_MAX_TIME );
Session_set_save_handler (
Array ($ sessHandler, '_ open '),
Array ($ sessHandler, '_ close '),
Array ($ sessHandler, '_ READ '),
Array ($ sessHandler, '_ write '),
Array ($ sessHandler, '_ destroy '),
Array ($ sessHandler, '_ clean ')
);
 
Session_start ();
Echo session_id ();
Echo $ _ SESSION ['name'] = 'hangzhou ';

> The first method is relatively simple, but the database number cannot be selected, and the prefix information of session_id is difficult to control. However, this method can also directly modify the php configuration file so that sessions of all programs use redis.
> The second method is relatively troublesome, but it is easy to use.
> When we talk about concurrency consistency on the Internet, all hash algorithms will certainly have this problem, but it can be solved to some extent through the ip Prefix hash combination.

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.