Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache client when it's fetched from first personalAccessClient #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

danialhamod
Copy link

In my project I'm using createToken function from HasApiTokens trait and laravel-passport-memoized is used to avoid duplicated db queries caused by Passport.
But there still a duplicated query as the following:

Screenshot 2024-04-01 at 2 05 40 pm

Both queries are called by ClientRepository class's functions, first one is called by find and second by personalAccessClient.
Currently, personalAccessClient is not caching, so this pull request add an override to this function in order to cache the client when fetched.

@stayallive
Copy link
Owner

Have you tested these changes? Because looking over the code I am unsure this works as you describe.

If you have set the config option passport.personal_access_client.id it should already use ->find which is already memoized. Do you have that config setup or have you left that empty?

@danialhamod
Copy link
Author

Have you tested these changes? Because looking over the code I am unsure this works as you describe.

If you have set the config option passport.personal_access_client.id it should already use ->find which is already memoized. Do you have that config setup or have you left that empty?

Sure I've tested code changes in my own project and it removes the query duplication.
Actually, setting the passport.personal_access_client.id resolved the problem in my case, thank you for reminding me of that.

@stayallive
Copy link
Owner

Interesting, I still wouldn't think this would remove the duplicate query... but maybe it's retrieved another way somewhere else. Anyway, give me some time to test this out and I'll merge it.

In the meantime explicitly setting the id of the personal access client in the config is a good workaround.

@danialhamod
Copy link
Author

danialhamod commented Apr 2, 2024

Interesting, I still wouldn't think this would remove the duplicate query... but maybe it's retrieved another way somewhere else. Anyway, give me some time to test this out and I'll merge it.

In the meantime explicitly setting the id of the personal access client in the config is a good workaround.

Don't worry, take your time.

When you're testing it plz consider the following:
Here is the function we are overriding in Passport (which calls the first query): personalAccessClient()

public function personalAccessClient()
{
    if ($this->personalAccessClientId) {
        return $this->find($this->personalAccessClientId);
    }

    $client = Passport::personalAccessClient();

    if (! $client->exists()) {
        throw new RuntimeException('Personal access client not found. Please create one.');
    }

    return $client->orderBy($client->getKeyName(), 'desc')->first()->client;
}

When config passport.personal_access_client.id isn't set, the $this->personalAccessClientId will be null so find won't be called (which means client will returned without being memoized) and this is the reason of query duplication.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants