Article From:https://www.cnblogs.com/fps2tao/p/9969244.html

laravelUse redis to save pits encountered by sessions.

 

Configuring redis storage session process is like this

In the. evn file, change session driver and connection to redis as follows:

SESSION_DRIVER=redis
SESSION_CONNECTION=session //This is a new addition.

Where is the session configuration file, change the corresponding link configuration (config/database.php) as follows:

    'redis' => [

        'client' => 'phpredis',

        'default' => [
            'host' => env('REDIS_HOST', '127.0.0.1'),
            'password' => env('REDIS_PASSWORD', null),
            'port' => env('REDIS_PORT', 6379),
            'database' => env('REDIS_DB', 0),
        ],

        'cache' => [
            'host' => env('REDIS_HOST', '127.0.0.1'),
            'password' => env('REDIS_PASSWORD', null),
            'port' => env('REDIS_PORT', 6379),
            'database' => env('REDIS_CACHE_DB', 1),
        ],

        'session' => [
            'host' => env('REDIS_HOST', '127.0.0.1'),
            'password' => env('REDIS_PASSWORD', null),
            'port' => env('REDIS_PORT', 6379),
            'database' => env('REDIS_CACHE_DB', 2),
        ],

    ],

Among them,’session’is the new link, which should have the same name as the. env configuration.

 

Then configure the test, the page is normal (can read and write), is not found in the redis-cli console session

    public function index(Request $request)
    {
//        $redis = new \Redis();
//        $test = $redis->connect('127.0.0.1',6379);
//        $redis->set('cat', 111);
//        //Gets the value of a string//echo $redis-> get ('cat'); and //111
        
        $redis = app('redis.connection');
        $redis->set('library', 'predis'); // Store keys as libraries and predis records;
        $request->session()->put('let','lw21321321t');
        $request->session()->put('lwt','lw888881t');
        $request->session()->put('ss','ss');
        
        return "aa11";
    }
    public function show(Request $request)
    {
        $sesson = $request->session();
        dd($sesson);
        return "show";

    }

Result:

 

But the redis-cli console never worked out.

After searching the Internet for a while, I found that redis has 16 databases. My session configuration exists in database 2, so I have to look at the data in database 2, and the result is that [awkward sweating]

select 2 //Switching to redis database 2

 

 

 Reference: https://www.cnblogs.com/oxspirt/p/6529791.html

 

Leave a Reply

Your email address will not be published. Required fields are marked *