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-duplicate-queries #1367

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions config/passport.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,20 @@

'public_key' => env('PASSPORT_PUBLIC_KEY'),


/*
|--------------------------------------------------------------------------
| Remove duplicate queries
|--------------------------------------------------------------------------
|
| By default, Passport doesn't cache queries .
| Change to true to increase performance
|
*/


'use_cache' => env('PASSPORT_USE_CACHE', false),

/*
|--------------------------------------------------------------------------
| Client UUIDs
Expand Down
60 changes: 60 additions & 0 deletions src/CacheTokenRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php


namespace Laravel\Passport;


use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\Cache;

class CacheTokenRepository extends TokenRepository
{

/**
* The key for caching queries.
*
* @var string
*/
protected $cache_key = 'passport-token';

/**
* Get a token by the given ID.
*
* @param string $id
* @return Token
*/
public function find($id)
{
return Cache::remember("$this->cache_key-id-$id", now()->addMinutes(30), function () use ($id) {
return parent::find($id);
});
}


/**
* Get a token by the given user ID and token ID.
*
* @param string $id
* @param int $userId
* @return Token|null
*/
public function findForUser($id, $userId)
{
return Cache::remember("$this->cache_key-id-$id-user-$userId", now()->addMinutes(30), function () use ($id, $userId) {
return parent::findForUser($id, $userId);
});
}

/**
* Get the token instances for the given user ID.
*
* @param mixed $userId
* @return Collection
*/
public function forUser($userId)
{
return Cache::remember("$this->cache_key-user-$userId", now()->addMinutes(30), function () use ($userId) {
return parent::forUser($userId);
});
}
}
38 changes: 26 additions & 12 deletions src/PassportServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,23 @@ class PassportServiceProvider extends ServiceProvider
*/
public function boot()
{
$this->loadViewsFrom(__DIR__.'/../resources/views', 'passport');
$this->loadViewsFrom(__DIR__ . '/../resources/views', 'passport');

$this->deleteCookieOnLogout();

if ($this->app->runningInConsole()) {
$this->registerMigrations();

$this->publishes([
__DIR__.'/../database/migrations' => database_path('migrations'),
__DIR__ . '/../database/migrations' => database_path('migrations'),
], 'passport-migrations');

$this->publishes([
__DIR__.'/../resources/views' => base_path('resources/views/vendor/passport'),
__DIR__ . '/../resources/views' => base_path('resources/views/vendor/passport'),
], 'passport-views');

$this->publishes([
__DIR__.'/../config/passport.php' => config_path('passport.php'),
__DIR__ . '/../config/passport.php' => config_path('passport.php'),
], 'passport-config');

$this->commands([
Expand All @@ -68,8 +68,8 @@ public function boot()
*/
protected function registerMigrations()
{
if (Passport::$runsMigrations && ! config('passport.client_uuids')) {
$this->loadMigrationsFrom(__DIR__.'/../database/migrations');
if (Passport::$runsMigrations && !config('passport.client_uuids')) {
$this->loadMigrationsFrom(__DIR__ . '/../database/migrations');
}
}

Expand All @@ -80,14 +80,16 @@ protected function registerMigrations()
*/
public function register()
{
$this->mergeConfigFrom(__DIR__.'/../config/passport.php', 'passport');
$this->mergeConfigFrom(__DIR__ . '/../config/passport.php', 'passport');

Passport::setClientUuids($this->app->make(Config::class)->get('passport.client_uuids', false));

$this->registerAuthorizationServer();
$this->registerClientRepository();
$this->registerResourceServer();
$this->registerGuard();
if (config('passport.use_cache') == true)
$this->registerCache();
}

/**
Expand Down Expand Up @@ -245,15 +247,15 @@ protected function registerResourceServer()
/**
* Create a CryptKey instance without permissions check.
*
* @param string $type
* @param string $type
* @return \League\OAuth2\Server\CryptKey
*/
protected function makeCryptKey($type)
{
$key = str_replace('\\n', "\n", $this->app->make(Config::class)->get('passport.'.$type.'_key'));
$key = str_replace('\\n', "\n", $this->app->make(Config::class)->get('passport.' . $type . '_key'));

if (! $key) {
$key = 'file://'.Passport::keyPath('oauth-'.$type.'.key');
if (!$key) {
$key = 'file://' . Passport::keyPath('oauth-' . $type . '.key');
}

return new CryptKey($key, null, false);
Expand All @@ -278,7 +280,7 @@ protected function registerGuard()
/**
* Make an instance of the token guard.
*
* @param array $config
* @param array $config
* @return \Illuminate\Auth\RequestGuard
*/
protected function makeGuard(array $config)
Expand Down Expand Up @@ -307,4 +309,16 @@ protected function deleteCookieOnLogout()
}
});
}

/**
* Bind CacheTokenRepository
*
* @return void
*/
protected function registerCache()
{
$this->app->bind(TokenRepository::class, function () {
return new CacheTokenRepository();
});
}
}