Skip to content

Commit

Permalink
Cache the currentUser (Implement #846)
Browse files Browse the repository at this point in the history
  • Loading branch information
lcharette committed Nov 13, 2018
1 parent fb0292f commit 522f99e
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 2 deletions.
14 changes: 14 additions & 0 deletions app/sprinkles/account/config/default.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 10 additions & 2 deletions app/sprinkles/account/src/Authenticate/Authenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
/**
* UserFrosting (http://www.userfrosting.com)
*
* @link https://github.com/userfrosting/UserFrosting
* @license https://github.com/userfrosting/UserFrosting/blob/master/licenses/UserFrosting.md (MIT License)
*/

namespace UserFrosting\Sprinkle\Account\Database\Models\Events;

use UserFrosting\Sprinkle\Core\Database\Models\Model;
use UserFrosting\Sprinkle\Core\Facades\Cache;
use UserFrosting\Sprinkle\Core\Facades\Config;

/**
* Event for global cache object deletion on model update
* @author Louis Charette
*/
class DeleteUserCacheEvent
{
/**
* @param Model $user
*/
public function __construct(Model $user)
{
$key = Config::get('cache.user.key');
Cache::forget($key . $user->id);
}
}
9 changes: 9 additions & 0 deletions app/sprinkles/account/src/Database/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down

0 comments on commit 522f99e

Please sign in to comment.