Add cache decorator for UserModel

This commit is contained in:
Frederic Guillot 2016-12-17 13:39:03 -05:00
parent aafa1de4d5
commit ddeb89e2c6
7 changed files with 73 additions and 5 deletions

View File

@ -31,7 +31,7 @@ class AuthenticationMiddleware extends Base implements MiddlewareInterface
$this->sessionStorage->scope = 'API'; $this->sessionStorage->scope = 'API';
if ($this->isUserAuthenticated($username, $password)) { if ($this->isUserAuthenticated($username, $password)) {
$this->userSession->initialize($this->userModel->getByUsername($username)); $this->userSession->initialize($this->userCacheDecorator->getByUsername($username));
} elseif (! $this->isAppAuthenticated($username, $password)) { } elseif (! $this->isAppAuthenticated($username, $password)) {
$this->logger->error('API authentication failure for '.$username); $this->logger->error('API authentication failure for '.$username);
throw new AuthenticationFailureException('Wrong credentials'); throw new AuthenticationFailureException('Wrong credentials');

View File

@ -45,7 +45,7 @@ class ReverseProxyAuth extends Base implements PreAuthenticationProviderInterfac
$username = $this->request->getRemoteUser(); $username = $this->request->getRemoteUser();
if (! empty($username)) { if (! empty($username)) {
$userProfile = $this->userModel->getByUsername($username); $userProfile = $this->userCacheDecorator->getByUsername($username);
$this->userInfo = new ReverseProxyUserProvider($username, $userProfile ?: array()); $this->userInfo = new ReverseProxyUserProvider($username, $userProfile ?: array());
return true; return true;
} }

View File

@ -109,7 +109,7 @@ class PasswordResetController extends BaseController
$token = $this->passwordResetModel->create($username); $token = $this->passwordResetModel->create($username);
if ($token !== false) { if ($token !== false) {
$user = $this->userModel->getByUsername($username); $user = $this->userCacheDecorator->getByUsername($username);
$this->emailClient->send( $this->emailClient->send(
$user['email'], $user['email'],

View File

@ -58,7 +58,8 @@ use Pimple\Container;
* @property \Kanboard\Core\Paginator $paginator * @property \Kanboard\Core\Paginator $paginator
* @property \Kanboard\Core\Template $template * @property \Kanboard\Core\Template $template
* @property \Kanboard\Decorator\MetadataCacheDecorator $userMetadataCacheDecorator * @property \Kanboard\Decorator\MetadataCacheDecorator $userMetadataCacheDecorator
* @property \Kanboard\Decorator\columnRestrictionCacheDecorator $columnRestrictionCacheDecorator * @property \Kanboard\Decorator\UserCacheDecorator $userCacheDecorator
* @property \Kanboard\Decorator\ColumnRestrictionCacheDecorator $columnRestrictionCacheDecorator
* @property \Kanboard\Decorator\ColumnMoveRestrictionCacheDecorator $columnMoveRestrictionCacheDecorator * @property \Kanboard\Decorator\ColumnMoveRestrictionCacheDecorator $columnMoveRestrictionCacheDecorator
* @property \Kanboard\Decorator\ProjectRoleRestrictionCacheDecorator $projectRoleRestrictionCacheDecorator * @property \Kanboard\Decorator\ProjectRoleRestrictionCacheDecorator $projectRoleRestrictionCacheDecorator
* @property \Kanboard\Model\ActionModel $actionModel * @property \Kanboard\Model\ActionModel $actionModel

View File

@ -88,7 +88,7 @@ class Markdown extends Parsedown
{ {
if (! $this->isPublicLink && preg_match('/^@([^\s,!:?]+)/', $Excerpt['text'], $matches)) { if (! $this->isPublicLink && preg_match('/^@([^\s,!:?]+)/', $Excerpt['text'], $matches)) {
$username = rtrim($matches[1], '.'); $username = rtrim($matches[1], '.');
$user = $this->container['userModel']->getByUsername($username); $user = $this->container['userCacheDecorator']->getByUsername($username);
if (! empty($user)) { if (! empty($user)) {
$url = $this->container['helper']->url->href('UserViewController', 'profile', array('user_id' => $user['id'])); $url = $this->container['helper']->url->href('UserViewController', 'profile', array('user_id' => $user['id']));

View File

@ -0,0 +1,59 @@
<?php
namespace Kanboard\Decorator;
use Kanboard\Core\Cache\CacheInterface;
use Kanboard\Model\UserModel;
/**
* Class UserCacheDecorator
*
* @package Kanboard\Decorator
* @author Frederic Guillot
*/
class UserCacheDecorator
{
protected $cachePrefix = 'user_model:';
/**
* @var CacheInterface
*/
protected $cache;
/**
* @var UserModel
*/
private $userModel;
/**
* UserCacheDecorator constructor.
*
* @param CacheInterface $cache
* @param UserModel $userModel
*/
public function __construct(CacheInterface $cache, UserModel $userModel)
{
$this->cache = $cache;
$this->userModel = $userModel;
}
/**
* Get a specific user by the username
*
* @access public
* @param string $username Username
* @return array
*/
public function getByUsername($username)
{
$key = $this->cachePrefix.$username;
$user = $this->cache->get($key);
if ($user === null) {
$user = $this->userModel->getByUsername($username);
$this->cache->set($key, $user);
}
return $user;
}
}

View File

@ -8,6 +8,7 @@ use Kanboard\Decorator\ColumnMoveRestrictionCacheDecorator;
use Kanboard\Decorator\ColumnRestrictionCacheDecorator; use Kanboard\Decorator\ColumnRestrictionCacheDecorator;
use Kanboard\Decorator\MetadataCacheDecorator; use Kanboard\Decorator\MetadataCacheDecorator;
use Kanboard\Decorator\ProjectRoleRestrictionCacheDecorator; use Kanboard\Decorator\ProjectRoleRestrictionCacheDecorator;
use Kanboard\Decorator\UserCacheDecorator;
use Pimple\Container; use Pimple\Container;
use Pimple\ServiceProviderInterface; use Pimple\ServiceProviderInterface;
@ -40,6 +41,13 @@ class CacheProvider implements ServiceProviderInterface
$container['cacheDriver'] = $container['memoryCache']; $container['cacheDriver'] = $container['memoryCache'];
} }
$container['userCacheDecorator'] = function($c) {
return new UserCacheDecorator(
$c['memoryCache'],
$c['userModel']
);
};
$container['userMetadataCacheDecorator'] = function($c) { $container['userMetadataCacheDecorator'] = function($c) {
return new MetadataCacheDecorator( return new MetadataCacheDecorator(
$c['cacheDriver'], $c['cacheDriver'],