diff options
author | Frederic Guillot <fred@kanboard.net> | 2016-12-17 13:39:03 -0500 |
---|---|---|
committer | Frederic Guillot <fred@kanboard.net> | 2016-12-17 13:39:03 -0500 |
commit | ddeb89e2c6622f197d1b7738042182b34d5054ed (patch) | |
tree | e7c473b9753d8ce309ec73f40c9eef62b2b9cc81 /app/Decorator | |
parent | aafa1de4d56b0791c4d367aa530587082c833faf (diff) |
Add cache decorator for UserModel
Diffstat (limited to 'app/Decorator')
-rw-r--r-- | app/Decorator/UserCacheDecorator.php | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/app/Decorator/UserCacheDecorator.php b/app/Decorator/UserCacheDecorator.php new file mode 100644 index 00000000..1cfe31c9 --- /dev/null +++ b/app/Decorator/UserCacheDecorator.php @@ -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; + } +} |