From d43d38d820e6dfe1f2ec0b4a2c0217ac3071b835 Mon Sep 17 00:00:00 2001 From: ctexthuang Date: Fri, 5 Sep 2025 14:12:56 +0800 Subject: [PATCH] feat : cache Adapter --- app/Controller/Test/AdapterTestController.php | 22 +++++++++-- app/Interface/Test/Adapter/CacheInterface.php | 12 ++++++ .../Test/Adapter/Cache/FileCacheAdapter.php | 39 +++++++++++++++++++ .../Test/Adapter/Cache/FileCacheService.php | 24 ++++++++++++ .../Test/Adapter/Cache/RedisCacheService.php | 30 ++++++++++++++ app/Service/Test/Adapter/CacheService.php | 10 ++++- config/autoload/dependencies.php | 7 ++++ 7 files changed, 140 insertions(+), 4 deletions(-) create mode 100644 app/Interface/Test/Adapter/CacheInterface.php create mode 100644 app/Service/Test/Adapter/Cache/FileCacheAdapter.php create mode 100644 app/Service/Test/Adapter/Cache/FileCacheService.php create mode 100644 app/Service/Test/Adapter/Cache/RedisCacheService.php diff --git a/app/Controller/Test/AdapterTestController.php b/app/Controller/Test/AdapterTestController.php index f525c8d..7ba2925 100644 --- a/app/Controller/Test/AdapterTestController.php +++ b/app/Controller/Test/AdapterTestController.php @@ -6,7 +6,6 @@ namespace App\Controller\Test; use App\Service\Test\Adapter\CacheService; use App\Service\Test\Adapter\PayService; -use App\Service\Test\AdapterTestService; use Hyperf\HttpServer\Annotation\Controller; use Hyperf\HttpServer\Annotation\RequestMapping; @@ -14,20 +13,37 @@ use Hyperf\HttpServer\Annotation\RequestMapping; class AdapterTestController { /** + * pay 适配器 * @return array */ #[RequestMapping(path: 'pay', methods: 'GET')] - public function pay() + public function pay(): array { return (new PayService)->handle(); } /** + * cache 适配器 => dependencies.php * @return array */ #[RequestMapping(path: 'cache', methods: 'GET')] - public function cache() + public function cache(): array { return (new CacheService)->handle(); } + + /** + * 适配器模式(Adapter Pattern)是一种结构型设计模式,它允许不兼容的接口之间能够协同工作。 + * 实现适配器模式的关键步骤: + * + * 定义目标接口(客户端期望的接口) + * 创建被适配的类(需要被适配的现有类) + * 创建适配器类,实现目标接口并包装被适配的类 + * 通过依赖注入容器管理适配器的创建和绑定 + * 适配器模式在以下场景特别有用: + * + * 需要使用现有类,但其接口与你的代码不兼容 + * 想要复用一些已经存在的子类,但这些子类缺少一些公共功能 + * 需要为多个不同的子系统提供统一的接口 + */ } diff --git a/app/Interface/Test/Adapter/CacheInterface.php b/app/Interface/Test/Adapter/CacheInterface.php new file mode 100644 index 0000000..751e309 --- /dev/null +++ b/app/Interface/Test/Adapter/CacheInterface.php @@ -0,0 +1,12 @@ +fileCache = $fileCache; + } + + /** + * @param string $key + * @return array + */ + public function get(string $key): array + { + return $this->fileCache->fetch($key); + } +} \ No newline at end of file diff --git a/app/Service/Test/Adapter/Cache/FileCacheService.php b/app/Service/Test/Adapter/Cache/FileCacheService.php new file mode 100644 index 0000000..9bc82d7 --- /dev/null +++ b/app/Service/Test/Adapter/Cache/FileCacheService.php @@ -0,0 +1,24 @@ + $key, + 'cache' => 'file', + 'msg' => 'file cache fetch success' + ]; + } +} \ No newline at end of file diff --git a/app/Service/Test/Adapter/Cache/RedisCacheService.php b/app/Service/Test/Adapter/Cache/RedisCacheService.php new file mode 100644 index 0000000..bd17d09 --- /dev/null +++ b/app/Service/Test/Adapter/Cache/RedisCacheService.php @@ -0,0 +1,30 @@ + $key, + 'cache' => 'redis', + 'msg' => 'redis cache fetch success', + ]; + } +} \ No newline at end of file diff --git a/app/Service/Test/Adapter/CacheService.php b/app/Service/Test/Adapter/CacheService.php index 3d8365e..cc5b766 100644 --- a/app/Service/Test/Adapter/CacheService.php +++ b/app/Service/Test/Adapter/CacheService.php @@ -11,15 +11,23 @@ declare(strict_types=1); namespace App\Service\Test\Adapter; +use App\Interface\Test\Adapter\CacheInterface; use App\Service\Test\TestBaseService; +use Hyperf\Di\Annotation\Inject; class CacheService extends TestBaseService { + /** + * @var CacheInterface + */ + #[Inject] + private CacheInterface $cache; + /** * @return array */ public function handle(): array { - return $this->return->success(); + return $this->return->success('success',$this->cache->get('test_key')); } } \ No newline at end of file diff --git a/config/autoload/dependencies.php b/config/autoload/dependencies.php index f46bd96..34f1f9b 100644 --- a/config/autoload/dependencies.php +++ b/config/autoload/dependencies.php @@ -9,5 +9,12 @@ declare(strict_types=1); * @contact group@hyperf.io * @license https://github.com/hyperf/hyperf/blob/master/LICENSE */ + +use App\Interface\Test\Adapter\CacheInterface; +use App\Service\Test\Adapter\Cache\FileCacheAdapter; +use App\Service\Test\Adapter\Cache\RedisCacheService; + return [ + CacheInterface::class => RedisCacheService::class, +// CacheInterface::class => FileCacheAdapter::class, ];