| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- <?php
- // +----------------------------------------------------------------------
- // | saiadmin [ saiadmin快速开发框架 ]
- // +----------------------------------------------------------------------
- // | Author: sai <1430792918@qq.com>
- // +----------------------------------------------------------------------
- declare(strict_types=1);
- namespace plugin\saiadmin\app\cache;
- use plugin\saiadmin\app\logic\system\SystemUserLogic;
- use support\think\Cache;
- /**
- * 用户信息缓存
- */
- class UserInfoCache
- {
- /**
- * 读取缓存配置
- * @return array
- */
- public static function cacheConfig(): array
- {
- return config('plugin.saiadmin.saithink.user_cache', [
- 'prefix' => 'saiadmin:user_cache:info_',
- 'expire' => 60 * 60 * 4,
- 'dept' => 'saiadmin:user_cache:dept_',
- 'role' => 'saiadmin:user_cache:role_',
- 'post' => 'saiadmin:user_cache:post_',
- ]);
- }
- /**
- * 通过id获取缓存管理员信息
- */
- public static function getUserInfo($uid): array
- {
- if (empty($uid)) {
- return [];
- }
- $cache = static::cacheConfig();
- // 直接从缓存获取
- $adminInfo = Cache::get($cache['prefix'] . $uid);
- if ($adminInfo) {
- return $adminInfo;
- }
- // 获取缓存信息并返回
- $adminInfo = static::setUserInfo($uid);
- if ($adminInfo) {
- return $adminInfo;
- }
- return [];
- }
- /**
- * 设置管理员信息
- */
- public static function setUserInfo($uid): array
- {
- $data = (new SystemUserLogic())->getUser($uid);
- $cache = static::cacheConfig();
- $tags = [];
- if (!empty($data['deptList'])) {
- $tags[] = $cache['dept'] . $data['deptList']['id'];
- }
- if (!empty($data['roleList'])) {
- foreach ($data['roleList'] as $role) {
- $tags[] = $cache['role'] . $role['id'];
- }
- }
- if (!empty($data['postList'])) {
- foreach ($data['postList'] as $post) {
- $tags[] = $cache['post'] . $post['id'];
- }
- }
- Cache::tag($tags)->set($cache['prefix'] . $uid, $data, $cache['expire']);
- return $data;
- }
- /**
- * 清理管理员信息缓存
- */
- public static function clearUserInfo($uid): bool
- {
- $cache = static::cacheConfig();
- return Cache::delete($cache['prefix'] . $uid);
- }
- /**
- * 清理部门下所有用户缓存
- */
- public static function clearUserInfoByDeptId($dept_id): bool
- {
- $cache = static::cacheConfig();
- if (is_array($dept_id)) {
- $tags = [];
- foreach ($dept_id as $id) {
- $tags[] = $cache['dept'] . $id;
- }
- } else {
- $tags = $cache['dept'] . $dept_id;
- }
- return Cache::tag($tags)->clear();
- }
- /**
- * 清理角色下所有用户缓存
- */
- public static function clearUserInfoByRoleId($role_id): bool
- {
- $cache = static::cacheConfig();
- if (is_array($role_id)) {
- $tags = [];
- foreach ($role_id as $id) {
- $tags[] = $cache['role'] . $id;
- }
- } else {
- $tags = $cache['role'] . $role_id;
- }
- return Cache::tag($tags)->clear();
- }
- /**
- * 清理岗位下所有用户缓存
- */
- public static function clearUserInfoByPostId($post_id): bool
- {
- $cache = static::cacheConfig();
- if (is_array($post_id)) {
- $tags = [];
- foreach ($post_id as $id) {
- $tags[] = $cache['post'] . $id;
- }
- } else {
- $tags = $cache['post'] . $post_id;
- }
- return Cache::tag($tags)->clear();
- }
- }
|