feat : cache Adapter
This commit is contained in:
@@ -6,7 +6,6 @@ namespace App\Controller\Test;
|
|||||||
|
|
||||||
use App\Service\Test\Adapter\CacheService;
|
use App\Service\Test\Adapter\CacheService;
|
||||||
use App\Service\Test\Adapter\PayService;
|
use App\Service\Test\Adapter\PayService;
|
||||||
use App\Service\Test\AdapterTestService;
|
|
||||||
use Hyperf\HttpServer\Annotation\Controller;
|
use Hyperf\HttpServer\Annotation\Controller;
|
||||||
use Hyperf\HttpServer\Annotation\RequestMapping;
|
use Hyperf\HttpServer\Annotation\RequestMapping;
|
||||||
|
|
||||||
@@ -14,20 +13,37 @@ use Hyperf\HttpServer\Annotation\RequestMapping;
|
|||||||
class AdapterTestController
|
class AdapterTestController
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
|
* pay 适配器
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
#[RequestMapping(path: 'pay', methods: 'GET')]
|
#[RequestMapping(path: 'pay', methods: 'GET')]
|
||||||
public function pay()
|
public function pay(): array
|
||||||
{
|
{
|
||||||
return (new PayService)->handle();
|
return (new PayService)->handle();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* cache 适配器 => dependencies.php
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
#[RequestMapping(path: 'cache', methods: 'GET')]
|
#[RequestMapping(path: 'cache', methods: 'GET')]
|
||||||
public function cache()
|
public function cache(): array
|
||||||
{
|
{
|
||||||
return (new CacheService)->handle();
|
return (new CacheService)->handle();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 适配器模式(Adapter Pattern)是一种结构型设计模式,它允许不兼容的接口之间能够协同工作。
|
||||||
|
* 实现适配器模式的关键步骤:
|
||||||
|
*
|
||||||
|
* 定义目标接口(客户端期望的接口)
|
||||||
|
* 创建被适配的类(需要被适配的现有类)
|
||||||
|
* 创建适配器类,实现目标接口并包装被适配的类
|
||||||
|
* 通过依赖注入容器管理适配器的创建和绑定
|
||||||
|
* 适配器模式在以下场景特别有用:
|
||||||
|
*
|
||||||
|
* 需要使用现有类,但其接口与你的代码不兼容
|
||||||
|
* 想要复用一些已经存在的子类,但这些子类缺少一些公共功能
|
||||||
|
* 需要为多个不同的子系统提供统一的接口
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|||||||
12
app/Interface/Test/Adapter/CacheInterface.php
Normal file
12
app/Interface/Test/Adapter/CacheInterface.php
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Interface\Test\Adapter;
|
||||||
|
|
||||||
|
interface CacheInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @param string $key
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function get(string $key): array;
|
||||||
|
}
|
||||||
39
app/Service/Test/Adapter/Cache/FileCacheAdapter.php
Normal file
39
app/Service/Test/Adapter/Cache/FileCacheAdapter.php
Normal 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\Adapter\Cache;
|
||||||
|
|
||||||
|
use App\Interface\Test\Adapter\CacheInterface;
|
||||||
|
|
||||||
|
class FileCacheAdapter implements CacheInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var FileCacheService $fileCache
|
||||||
|
*/
|
||||||
|
private FileCacheService $fileCache;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param FileCacheService $fileCache
|
||||||
|
*/
|
||||||
|
public function __construct(FileCacheService $fileCache)
|
||||||
|
{
|
||||||
|
$this->fileCache = $fileCache;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $key
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function get(string $key): array
|
||||||
|
{
|
||||||
|
return $this->fileCache->fetch($key);
|
||||||
|
}
|
||||||
|
}
|
||||||
24
app/Service/Test/Adapter/Cache/FileCacheService.php
Normal file
24
app/Service/Test/Adapter/Cache/FileCacheService.php
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
<?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\Adapter\Cache;
|
||||||
|
|
||||||
|
class FileCacheService
|
||||||
|
{
|
||||||
|
public function fetch(string $key): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'key' => $key,
|
||||||
|
'cache' => 'file',
|
||||||
|
'msg' => 'file cache fetch success'
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
30
app/Service/Test/Adapter/Cache/RedisCacheService.php
Normal file
30
app/Service/Test/Adapter/Cache/RedisCacheService.php
Normal 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\Adapter\Cache;
|
||||||
|
|
||||||
|
use App\Interface\Test\Adapter\CacheInterface;
|
||||||
|
|
||||||
|
class RedisCacheService implements CacheInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @param string $key
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function get(string $key): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'key' => $key,
|
||||||
|
'cache' => 'redis',
|
||||||
|
'msg' => 'redis cache fetch success',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -11,15 +11,23 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace App\Service\Test\Adapter;
|
namespace App\Service\Test\Adapter;
|
||||||
|
|
||||||
|
use App\Interface\Test\Adapter\CacheInterface;
|
||||||
use App\Service\Test\TestBaseService;
|
use App\Service\Test\TestBaseService;
|
||||||
|
use Hyperf\Di\Annotation\Inject;
|
||||||
|
|
||||||
class CacheService extends TestBaseService
|
class CacheService extends TestBaseService
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* @var CacheInterface
|
||||||
|
*/
|
||||||
|
#[Inject]
|
||||||
|
private CacheInterface $cache;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function handle(): array
|
public function handle(): array
|
||||||
{
|
{
|
||||||
return $this->return->success();
|
return $this->return->success('success',$this->cache->get('test_key'));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -9,5 +9,12 @@ declare(strict_types=1);
|
|||||||
* @contact group@hyperf.io
|
* @contact group@hyperf.io
|
||||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
* @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 [
|
return [
|
||||||
|
CacheInterface::class => RedisCacheService::class,
|
||||||
|
// CacheInterface::class => FileCacheAdapter::class,
|
||||||
];
|
];
|
||||||
|
|||||||
Reference in New Issue
Block a user