75 lines
1.6 KiB
PHP
75 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Cache\Redis\Admin;
|
|
|
|
use App\Cache\Redis\RedisCache;
|
|
use App\Constants\RedisCode;
|
|
use App\Model\AdminMenu;
|
|
use App\Service\ServiceTrait\Admin\AdminRoleMenuTrait;
|
|
use Hyperf\Di\Annotation\Inject;
|
|
use Psr\Container\ContainerExceptionInterface;
|
|
use Psr\Container\NotFoundExceptionInterface;
|
|
use RedisException;
|
|
|
|
class MenuCache
|
|
{
|
|
use AdminRoleMenuTrait;
|
|
|
|
/**
|
|
* @var RedisCache $redis
|
|
*/
|
|
#[Inject]
|
|
protected RedisCache $redis;
|
|
|
|
/**
|
|
* @var AdminMenu
|
|
*/
|
|
#[Inject]
|
|
protected AdminMenu $adminMenuModel;
|
|
|
|
/**
|
|
* 菜单
|
|
* @var string
|
|
*/
|
|
protected string $menuKey;
|
|
|
|
/**
|
|
* 构造函数注入 key
|
|
*/
|
|
public function __construct()
|
|
{
|
|
$this->menuKey = AdminRedisKey::adminMenuList();
|
|
}
|
|
|
|
/**
|
|
* @return array
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
* @throws RedisException
|
|
*/
|
|
public function getMenu(): array
|
|
{
|
|
if ($this->redis->exists($this->menuKey,RedisCode::SYSTEM_DB)) {
|
|
return json_decode($this->redis->get($this->menuKey,RedisCode::SYSTEM_DB),true);
|
|
}
|
|
|
|
$allMenuList = $this->adminMenuModel->getAllMenu();
|
|
|
|
$data = $this->getDbMenu($allMenuList);
|
|
|
|
$this->redis->set($this->menuKey,json_encode($data),RedisCode::SYSTEM_DB);
|
|
|
|
return $data;
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
* @throws RedisException
|
|
*/
|
|
public function delMenu(): void
|
|
{
|
|
$this->redis->delete($this->menuKey,RedisCode::SYSTEM_DB);
|
|
}
|
|
} |