1

I want to store the session to redis in Laravel.

I was do : - Change the session driver into 'redis' - Set my redis server - and then use this code to store $req->session()->put($email, json_encode($user));

The code was run successfully. And it was store to redis. But, I just simply add the code to 1 function.

Why the other function like example testing()/check(), also setex to redis? I don't even put the code into that function.

  • I understand now. Why they always fill the redis for every request. Because on every route(my laravel version is 5.2) on my list, that already include middleware web. https://stackoverflow.com/questions/37417593/avoid-remove-web-middleware-in-routes-for-laravel-5-2-31 – Chandra Putra Wijaya Nov 14 '17 at 06:46

2 Answers2

1

You can just set the session driver to redis (in your .env file) and use

session(['key', $val]); 

to store session values

and

session('key'); 

to retrieve them

online Thomas
  • 8,864
  • 6
  • 44
  • 85
  • Yep, that was success. But, it will also store for every method who hit by users. I found that, that because 'web' middleware. But now, how to get the data if when i want to get, it also re-set the data to redis. So, if my data before has expired 6090 seconds. After I hit(for get the data), it will re-set again to 7200(default) – Chandra Putra Wijaya Nov 14 '17 at 07:25
0

You can store data in Redis this way :-

use Illuminate\Support\Facades\Redis; // add in top of controler before class
      or
use Redis;
//put this in function
$redis = new Redis();
$redis->set('boo','Have beer and relax!')
$redis->get('boo');
kunal
  • 4,122
  • 12
  • 40
  • 75