diff options
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; + } +} |