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

@@ -8,6 +8,8 @@ use App\Service\Test\Proxy\AopService;
use App\Service\Test\Proxy\BasicSubjectService;
use App\Service\Test\Proxy\DynamicProxyService;
use App\Service\Test\Proxy\Payment\PaymentService;
use App\Service\Test\Proxy\UserInfoService;
use App\Service\Test\Proxy\UserService;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Psr\Container\ContainerExceptionInterface;
@@ -17,6 +19,7 @@ use Psr\Container\NotFoundExceptionInterface;
class ProxyController
{
/**
* 静态代理
* @return array
*/
#[RequestMapping(path: 'basic', methods: 'GET')]
@@ -26,6 +29,7 @@ class ProxyController
}
/**
* 动态代理
* @return array
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
@@ -37,6 +41,7 @@ class ProxyController
}
/**
* aop代理
* @return array
*/
#[RequestMapping(path: 'payment', methods: 'GET')]
@@ -44,4 +49,19 @@ class ProxyController
{
return (new AopService)->handle();
}
/**
* 缓存代理
* @return array
*/
#[RequestMapping(path: 'get_user_info', methods: 'GET')]
public function getUserInfo(): array
{
return (new UserService)->handle();
}
public function remote()
{
}
}

View File

@@ -0,0 +1,13 @@
<?php
namespace App\Interface\Test\Proxy;
interface UserInfoInterface
{
/**
* 用户信息接口
* @param int $userId
* @return array
*/
public function getUserInfo(int $userId): array;
}

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)
)
);
}
}