feat : cache Adapter
This commit is contained in:
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;
|
||||
|
||||
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'));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user