From 522f99e57a0980bc73aab4d836adb480f29da8b7 Mon Sep 17 00:00:00 2001 From: Louis Charette Date: Mon, 12 Nov 2018 22:02:27 -0500 Subject: [PATCH] Cache the `currentUser` (Implement #846) --- app/sprinkles/account/config/default.php | 14 +++++++++ .../src/Authenticate/Authenticator.php | 12 ++++++-- .../Models/Events/DeleteUserCacheEvent.php | 29 +++++++++++++++++++ .../account/src/Database/Models/User.php | 9 ++++++ 4 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 app/sprinkles/account/src/Database/Models/Events/DeleteUserCacheEvent.php diff --git a/app/sprinkles/account/config/default.php b/app/sprinkles/account/config/default.php index fb811fcf1..24cc64644 100644 --- a/app/sprinkles/account/config/default.php +++ b/app/sprinkles/account/config/default.php @@ -10,6 +10,20 @@ * Account configuration file for UserFrosting. */ return [ + /* + * ---------------------------------------------------------------------- + * User Cache Config + * ---------------------------------------------------------------------- + * Cache current user info for a given time to speed up process. + * Set to zero to disable. + */ + 'cache' => [ + 'user' => [ + 'delay' => 120, // In minutes + 'key' => '_user', + ], + ], + /* * ---------------------------------------------------------------------- * AuthorizationManager Debug diff --git a/app/sprinkles/account/src/Authenticate/Authenticator.php b/app/sprinkles/account/src/Authenticate/Authenticator.php index 2383a4e04..c00c91f2f 100644 --- a/app/sprinkles/account/src/Authenticate/Authenticator.php +++ b/app/sprinkles/account/src/Authenticate/Authenticator.php @@ -92,7 +92,7 @@ class Authenticator * @param Session $session The session wrapper object that will store the user's id. * @param Config $config Config object that contains authentication settings. * @param Cache $cache Cache service instance - * @param Capsule $capsule Database service instance + * @param Capsule $db Database service instance */ public function __construct(ClassMapper $classMapper, Session $session, Config $config, Cache $cache, Capsule $db) { @@ -255,6 +255,9 @@ public function logout($complete = false) if ($currentUser) { $currentUser->onLogout(); } + + // Delete user object cache + $this->cache->forget($this->config['cache.user.key'] . $currentUserId); } $this->user = null; @@ -403,7 +406,12 @@ protected function validateRememberMeCookie() protected function validateUserAccount($userId) { if ($userId) { - $user = $this->classMapper->staticMethod('user', 'find', (int) $userId); + + // Load user from db, cache the result + $key = $this->config['cache.user.key'] . $userId; + $user = $this->cache->remember($key, $this->config['cache.user.delay'], function () use ($userId) { + return $this->classMapper->staticMethod('user', 'find', (int) $userId); + }); // If the user doesn't exist any more, throw an exception. if (!$user) { diff --git a/app/sprinkles/account/src/Database/Models/Events/DeleteUserCacheEvent.php b/app/sprinkles/account/src/Database/Models/Events/DeleteUserCacheEvent.php new file mode 100644 index 000000000..4e397f019 --- /dev/null +++ b/app/sprinkles/account/src/Database/Models/Events/DeleteUserCacheEvent.php @@ -0,0 +1,29 @@ +id); + } +} diff --git a/app/sprinkles/account/src/Database/Models/User.php b/app/sprinkles/account/src/Database/Models/User.php index bb98db7fe..5afd7d7e8 100644 --- a/app/sprinkles/account/src/Database/Models/User.php +++ b/app/sprinkles/account/src/Database/Models/User.php @@ -92,6 +92,15 @@ class User extends Model implements UserInterface 'full_name' ]; + /** + * Events used to handle the user object cache on update and deletion + * @var array + */ + protected $events = [ + 'saved' => Events\DeleteUserCacheEvent::class, + 'deleted' => Events\DeleteUserCacheEvent::class + ]; + /** * Cached dictionary of permissions for the user. *