From 1661f5dd01a1f0eb04765c22fa613b442ad5f813 Mon Sep 17 00:00:00 2001 From: sepehr Date: Mon, 12 Oct 2020 12:47:12 +0330 Subject: [PATCH] cache-duplicate-queries --- config/passport.php | 14 ++++++++ src/CacheTokenRepository.php | 60 +++++++++++++++++++++++++++++++++ src/PassportServiceProvider.php | 38 ++++++++++++++------- 3 files changed, 100 insertions(+), 12 deletions(-) create mode 100644 src/CacheTokenRepository.php diff --git a/config/passport.php b/config/passport.php index 162f1f84a..06dcfa794 100644 --- a/config/passport.php +++ b/config/passport.php @@ -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 diff --git a/src/CacheTokenRepository.php b/src/CacheTokenRepository.php new file mode 100644 index 000000000..4c7a33ba4 --- /dev/null +++ b/src/CacheTokenRepository.php @@ -0,0 +1,60 @@ +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); + }); + } +} diff --git a/src/PassportServiceProvider.php b/src/PassportServiceProvider.php index 3f4271285..2cfb42052 100644 --- a/src/PassportServiceProvider.php +++ b/src/PassportServiceProvider.php @@ -32,7 +32,7 @@ class PassportServiceProvider extends ServiceProvider */ public function boot() { - $this->loadViewsFrom(__DIR__.'/../resources/views', 'passport'); + $this->loadViewsFrom(__DIR__ . '/../resources/views', 'passport'); $this->deleteCookieOnLogout(); @@ -40,15 +40,15 @@ public function boot() $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([ @@ -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'); } } @@ -80,7 +80,7 @@ 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)); @@ -88,6 +88,8 @@ public function register() $this->registerClientRepository(); $this->registerResourceServer(); $this->registerGuard(); + if (config('passport.use_cache') == true) + $this->registerCache(); } /** @@ -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); @@ -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) @@ -307,4 +309,16 @@ protected function deleteCookieOnLogout() } }); } + + /** + * Bind CacheTokenRepository + * + * @return void + */ + protected function registerCache() + { + $this->app->bind(TokenRepository::class, function () { + return new CacheTokenRepository(); + }); + } }