Files
hyperf_service/app/Cache/Redis/Admin/SectionCache.php
2025-01-21 16:20:16 +08:00

76 lines
1.6 KiB
PHP

<?php
namespace App\Cache\Redis\Admin;
use App\Cache\Redis\RedisCache;
use App\Constants\RedisCode;
use App\Model\AdminSection;
use App\Service\ServiceTrait\Admin\SectionTrait;
use Hyperf\Di\Annotation\Inject;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use RedisException;
class SectionCache
{
use SectionTrait;
/**
* @var AdminSection
*/
#[Inject]
protected AdminSection $adminSectionModel;
/**
* @var RedisCache $redis
*/
#[Inject]
protected RedisCache $redis;
/**
* 菜单
* @var string
*/
protected string $key;
/**
* 构造函数注入 key
*/
public function __construct()
{
$this->key = AdminRedisKey::adminSectionList();
}
/**
* @return array
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws RedisException
*/
public function getList(): array
{
if ($this->redis->exists($this->key,RedisCode::SYSTEM_DB)) {
return json_decode($this->redis->get($this->key,RedisCode::SYSTEM_DB),true);
}
$allList = $this->adminSectionModel->getList();
$data = $this->getDbList($allList);
$this->redis->set($this->key,json_encode($data),RedisCode::SYSTEM_DB);
return $data;
}
/**
* @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws RedisException
*/
public function delList()
{
$this->redis->delete($this->key,RedisCode::SYSTEM_DB);
}
}