mirror of
https://gitee.com/ctexthuang/hyperf_rbac_framework_server_ctexthuang.git
synced 2025-12-25 19:27:48 +08:00
feat : common redis cache and common logger
This commit is contained in:
50
app/Trait/AdminUserTrait.php
Normal file
50
app/Trait/AdminUserTrait.php
Normal file
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace App\Trait;
|
||||
|
||||
use App\Cache\Redis\RedisCache;
|
||||
use App\Cache\Redis\RedisKey;
|
||||
use App\Repository\AdminUserRepository;
|
||||
use Hyperf\Context\Context;
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
|
||||
trait AdminUserTrait
|
||||
{
|
||||
/**
|
||||
* @var RedisCache
|
||||
*/
|
||||
#[Inject]
|
||||
protected RedisCache $redis;
|
||||
|
||||
/**
|
||||
* @var AdminUserRepository
|
||||
*/
|
||||
#[Inject]
|
||||
protected AdminUserRepository $adminUserRepository;
|
||||
|
||||
/**
|
||||
* @param int $adminId
|
||||
* @return array|null
|
||||
*/
|
||||
public function getAdminUserInfo(int $adminId): array|null
|
||||
{
|
||||
$key = RedisKey::getAdminUserInfoKey($adminId);
|
||||
if (Context::has($key)) {
|
||||
return Context::get($key,false);
|
||||
}
|
||||
|
||||
if ($this->redis->with()->exists($key)) {
|
||||
$userInfo = $this->redis->with()->get($key);
|
||||
Context::set($key,$userInfo);
|
||||
return json_decode($userInfo,true);
|
||||
}
|
||||
|
||||
$userInfo = $this->adminUserRepository->findById($adminId);
|
||||
if (!$userInfo) return null;
|
||||
|
||||
Context::set($key, $userInfo);
|
||||
$this->redis->with()->set($key, json_encode($userInfo), 3600);
|
||||
|
||||
return $userInfo;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user