69 lines
1.7 KiB
PHP
69 lines
1.7 KiB
PHP
<?php
|
|
/**
|
|
* This service file is part of item.
|
|
*
|
|
* @author ctexthuang
|
|
* @contact ctexthuang@qq.com
|
|
* @web_site https://ctexthuang.com
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Service\Test\Proxy\Cache;
|
|
|
|
use App\Interface\Test\Decorator\LoggerInterface;
|
|
use App\Interface\Test\Proxy\UserInfoInterface;
|
|
use Hyperf\Cache\Cache;
|
|
use Psr\SimpleCache\InvalidArgumentException;
|
|
|
|
class CacheUserInfoService implements UserInfoInterface
|
|
{
|
|
/**
|
|
* @var UserInfoService
|
|
*/
|
|
private UserInfoService $userInfoService;
|
|
|
|
/**
|
|
* @var Cache
|
|
*/
|
|
private Cache $cache;
|
|
|
|
/**
|
|
* @var LoggerInterface
|
|
*/
|
|
private LoggerInterface $logger;
|
|
|
|
/**
|
|
* @param UserInfoService $userInfoService
|
|
* @param LoggerInterface $logger
|
|
* @param Cache $cache
|
|
*/
|
|
public function __construct(UserInfoService $userInfoService, LoggerInterface $logger, Cache $cache)
|
|
{
|
|
$this->userInfoService = $userInfoService;
|
|
$this->logger = $logger;
|
|
$this->cache = $cache;
|
|
}
|
|
|
|
/**
|
|
* @param int $userId
|
|
* @return array
|
|
* @throws InvalidArgumentException
|
|
*/
|
|
public function getUserInfo(int $userId): array
|
|
{
|
|
$userKey = 'list:user:' . $userId;
|
|
|
|
if ($this->cache->has($userKey)) {
|
|
// echo "Getting user {$userId} from cache\n";
|
|
$this->logger->log('Getting user ' . $userId . ' from cache' . PHP_EOL);
|
|
return $this->cache->get($userKey);
|
|
}
|
|
|
|
$this->logger->log('Getting user ' . $userId . ' from service' . PHP_EOL);
|
|
$userInfo = $this->userInfoService->getUserInfo($userId);
|
|
$this->cache->set($userKey, $userInfo, 3600);
|
|
|
|
return $userInfo;
|
|
}
|
|
} |