0

i have this function :

   public function showProfile($id)
    {

        // Check if user already in redis with cache key user.1 for example
        if (!Redis::exists('user.' . $id)) {
            // If the user is not in redis, fetch it from DB and add it in redis for 60 seconds before returning it
            $user = User::findOrFail($id);
            Redis::set('user.' . $user->id, $user);

            return $user;
        } else {
            // If user is in redis, just return it without querying the database
            return Redis::get('user'.$id);// How to set this?

        }

enter image description here

But the function return is null, why please...

  • Is there any reason you want to use redis directly instead of the [cache](https://laravel.com/docs/master/cache) with the redis driver? – Remul Apr 07 '20 at 08:49
  • Like Remul said, there is not point of using Redis directly. Use cache with redis driver. If you already have cache on different driver and want to use Redis only for this purpose, then simply use cache with multiple drivers. – chojnicki Apr 07 '20 at 08:54

2 Answers2

0

It looks like your keys are different. You are setting user.id:

Redis::set('user.' . $user->id, $user);

And you are trying to get userid:

return Redis::get('user'.$id);
Spholt
  • 3,724
  • 1
  • 18
  • 29
  • Don't worry, everyone does it haha. I've spent hours debugging something to find out i've spelt something wrong or left out a `;`. Good luck! – Spholt Apr 07 '20 at 10:02
0

This method worked for me

use Illuminate\Support\Facades\Redis;

$redisData = Redis::lrange("REDIS_KEY", 0, -1);
dd($redisData);
Akbarali
  • 688
  • 7
  • 16