feat : Proxy get user info

This commit is contained in:
2025-09-07 13:10:17 +08:00
parent 705d18239f
commit 32bc27596e
7 changed files with 186 additions and 0 deletions

View File

@@ -0,0 +1,69 @@
<?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;
}
}

View File

@@ -0,0 +1,30 @@
<?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\Proxy\UserInfoInterface;
class UserInfoService implements UserInfoInterface
{
/**
* @param int $userId
* @return array
*/
public function getUserInfo(int $userId): array
{
return [
'userId' => $userId,
'name' => 'User name'.$userId,
'email' => 'user'.$userId.'@email.com',
];
}
}

View File

@@ -0,0 +1,39 @@
<?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;
use App\Interface\Test\Proxy\UserInfoInterface;
use App\Service\Test\Proxy\Cache\UserInfoService;
use App\Service\Test\TestBaseService;
use Hyperf\Di\Annotation\Inject;
class UserService extends TestBaseService
{
/**
* @var UserInfoService
*/
#[Inject]
protected UserInfoInterface $userInfoInterface;
/**
* @return array
*/
public function handle(): array
{
return $this->return->success(
'success',
$this->userInfoInterface->getUserInfo(
(int)$this->request->input('user_id',1)
)
);
}
}