Files
hyperf_service/app/Cache/Redis/Admin/MenuCache.php
2024-10-28 17:56:19 +08:00

75 lines
1.6 KiB
PHP

<?php
namespace App\Cache\Redis\Admin;
use App\Cache\Redis\RedisCache;
use App\Constants\Admin\AuthCode;
use App\Model\AdminMenu;
use App\Service\ServiceTrait\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,'system')) {
return json_decode($this->redis->get($this->menuKey,'system'),true);
}
$allMenuList = $this->adminMenuModel->getAllMenu();
$data = $this->getDbMenu($allMenuList);
$this->redis->set($this->menuKey,json_encode($data),'system');
return $data;
}
/**
* @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws RedisException
*/
public function delMenu(): void
{
$this->redis->delete($this->menuKey,'system');
}
}