From 32bc27596e076430e20c82bbf7181bd3b7f762cd Mon Sep 17 00:00:00 2001 From: ctexthuang Date: Sun, 7 Sep 2025 13:10:17 +0800 Subject: [PATCH] feat : Proxy get user info --- app/Controller/Test/ProxyController.php | 20 ++++++ .../Test/Proxy/UserInfoInterface.php | 13 ++++ .../Test/Proxy/Cache/CacheUserInfoService.php | 69 +++++++++++++++++++ .../Test/Proxy/Cache/UserInfoService.php | 30 ++++++++ app/Service/Test/Proxy/UserService.php | 39 +++++++++++ config/autoload/dependencies.php | 10 +++ request/test.http | 5 ++ 7 files changed, 186 insertions(+) create mode 100644 app/Interface/Test/Proxy/UserInfoInterface.php create mode 100644 app/Service/Test/Proxy/Cache/CacheUserInfoService.php create mode 100644 app/Service/Test/Proxy/Cache/UserInfoService.php create mode 100644 app/Service/Test/Proxy/UserService.php diff --git a/app/Controller/Test/ProxyController.php b/app/Controller/Test/ProxyController.php index 2653168..930fa91 100644 --- a/app/Controller/Test/ProxyController.php +++ b/app/Controller/Test/ProxyController.php @@ -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() + { + + } } diff --git a/app/Interface/Test/Proxy/UserInfoInterface.php b/app/Interface/Test/Proxy/UserInfoInterface.php new file mode 100644 index 0000000..f9b0fd2 --- /dev/null +++ b/app/Interface/Test/Proxy/UserInfoInterface.php @@ -0,0 +1,13 @@ +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; + } +} \ No newline at end of file diff --git a/app/Service/Test/Proxy/Cache/UserInfoService.php b/app/Service/Test/Proxy/Cache/UserInfoService.php new file mode 100644 index 0000000..eeab53f --- /dev/null +++ b/app/Service/Test/Proxy/Cache/UserInfoService.php @@ -0,0 +1,30 @@ + $userId, + 'name' => 'User name'.$userId, + 'email' => 'user'.$userId.'@email.com', + ]; + } +} \ No newline at end of file diff --git a/app/Service/Test/Proxy/UserService.php b/app/Service/Test/Proxy/UserService.php new file mode 100644 index 0000000..fd0d9c0 --- /dev/null +++ b/app/Service/Test/Proxy/UserService.php @@ -0,0 +1,39 @@ +return->success( + 'success', + $this->userInfoInterface->getUserInfo( + (int)$this->request->input('user_id',1) + ) + ); + } +} \ No newline at end of file diff --git a/config/autoload/dependencies.php b/config/autoload/dependencies.php index 67dbf32..ef8801c 100644 --- a/config/autoload/dependencies.php +++ b/config/autoload/dependencies.php @@ -13,6 +13,7 @@ declare(strict_types=1); use App\Interface\Test\Adapter\CacheInterface; use App\Interface\Test\Decorator\HttpClientInterface; use App\Interface\Test\Decorator\LoggerInterface; +use App\Interface\Test\Proxy\UserInfoInterface; use App\Lib\Request\GuzzleHttpClient; use App\Service\Test\Adapter\Cache\RedisCacheService; use App\Service\Test\Decorator\Container\BasicFileLogger; @@ -21,6 +22,9 @@ use App\Service\Test\Decorator\Container\IpLoggerDecorator; use App\Service\Test\Decorator\Container\TimestampLoggerDecorator; use App\Service\Test\Decorator\Http\LoggableHttpClientService; use App\Service\Test\Decorator\Http\RetryableHttpClientService; +use App\Service\Test\Proxy\Cache\CacheUserInfoService; +use App\Service\Test\Proxy\Cache\UserInfoService; +use Hyperf\Cache\Cache; use function Hyperf\Support\make; return [ @@ -37,5 +41,11 @@ return [ $logger = make(BasicFileLogger::class); $client = make(RetryableHttpClientService::class,['httpClient' => $client , 'logger' => $logger , 'maxRetries' => 2]); return make(LoggableHttpClientService::class,['httpClient' => $client , 'logger' => $logger]); + }, + UserInfoInterface::class => function () { + $service = make(UserInfoService::class); + $logger = make(BasicFileLogger::class); + $cache = make(Cache::class); + return make(CacheUserInfoService::class, ['userInfoService' => $service,'logger' => $logger , 'cache' => $cache]); } ]; diff --git a/request/test.http b/request/test.http index af788ba..4726470 100644 --- a/request/test.http +++ b/request/test.http @@ -45,4 +45,9 @@ Content-Type: application/x-www-form-urlencoded ### Proxy payment test GET {{host}}/proxy/test/payment +Content-Type: application/x-www-form-urlencoded + + +### Proxy get_user_info test +GET {{host}}/proxy/test/get_user_info?user_id=2 Content-Type: application/x-www-form-urlencoded \ No newline at end of file