laravelredispredis

Laravel Redis Predis and method GET


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...


Solution

  • 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);