Reputation: 1011
I'm using Laravel 5.7. Redis is set up as my cache and and session driver.
In my controller, if I write to my session using either $request->session()->put() OR Session::put(), it will show up when I print my cache for the session id:
print_r(unserialize(Cache::get(Session::getId())));
Note, my primary goal in outputting session data this way is to access sessions that are not my active session.
However, if I write to my session using either of these methods down the line in my domain, they will NOT show up in the cache print, but will show up (along with everything else that does show up in the cache print) if I use:
print_r(Session::all());
I'm perplexed as to what is going on. I verified that what I'm saying is also true when accessing data via the redis cli. Apparently if I write to session in the domain, it is stored somewhere else somehow? Thoughts?
Upvotes: 1
Views: 747
Reputation: 6432
Your cache and your session are two totally different entities, even though they can use the same driver.
A session is used to maintain a relationship with a user. See the PHP session documentation for more details.
A cache is a place where you store and look for commonly accessed data, instead of hitting expensive code or the database. It is ephemeral, and is never guaranteed to hold any data whatsoever.
Why don't you just use Session::get('whatever')
?
After seeing your edit:
Note, my primary goal in outputting session data this way is to access sessions that are not my active session.
You should store that data via a more durable storage mechanism than your cache. As I mentioned before, a cache is ephemeral. You can use redis, as it's a key-value store and not a true cache like memcached. Just use it directly as such:
Redis::get('user:profile:'.$id);
Upvotes: 1