UserInfoCache.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | saiadmin [ saiadmin快速开发框架 ]
  4. // +----------------------------------------------------------------------
  5. // | Author: sai <1430792918@qq.com>
  6. // +----------------------------------------------------------------------
  7. declare(strict_types=1);
  8. namespace plugin\saiadmin\app\cache;
  9. use plugin\saiadmin\app\logic\system\SystemUserLogic;
  10. use support\think\Cache;
  11. /**
  12. * 用户信息缓存
  13. */
  14. class UserInfoCache
  15. {
  16. /**
  17. * 读取缓存配置
  18. * @return array
  19. */
  20. public static function cacheConfig(): array
  21. {
  22. return config('plugin.saiadmin.saithink.user_cache', [
  23. 'prefix' => 'saiadmin:user_cache:info_',
  24. 'expire' => 60 * 60 * 4,
  25. 'dept' => 'saiadmin:user_cache:dept_',
  26. 'role' => 'saiadmin:user_cache:role_',
  27. 'post' => 'saiadmin:user_cache:post_',
  28. ]);
  29. }
  30. /**
  31. * 通过id获取缓存管理员信息
  32. */
  33. public static function getUserInfo($uid): array
  34. {
  35. if (empty($uid)) {
  36. return [];
  37. }
  38. $cache = static::cacheConfig();
  39. // 直接从缓存获取
  40. $adminInfo = Cache::get($cache['prefix'] . $uid);
  41. if ($adminInfo) {
  42. return $adminInfo;
  43. }
  44. // 获取缓存信息并返回
  45. $adminInfo = static::setUserInfo($uid);
  46. if ($adminInfo) {
  47. return $adminInfo;
  48. }
  49. return [];
  50. }
  51. /**
  52. * 设置管理员信息
  53. */
  54. public static function setUserInfo($uid): array
  55. {
  56. $data = (new SystemUserLogic())->getUser($uid);
  57. $cache = static::cacheConfig();
  58. $tags = [];
  59. if (!empty($data['deptList'])) {
  60. $tags[] = $cache['dept'] . $data['deptList']['id'];
  61. }
  62. if (!empty($data['roleList'])) {
  63. foreach ($data['roleList'] as $role) {
  64. $tags[] = $cache['role'] . $role['id'];
  65. }
  66. }
  67. if (!empty($data['postList'])) {
  68. foreach ($data['postList'] as $post) {
  69. $tags[] = $cache['post'] . $post['id'];
  70. }
  71. }
  72. Cache::tag($tags)->set($cache['prefix'] . $uid, $data, $cache['expire']);
  73. return $data;
  74. }
  75. /**
  76. * 清理管理员信息缓存
  77. */
  78. public static function clearUserInfo($uid): bool
  79. {
  80. $cache = static::cacheConfig();
  81. return Cache::delete($cache['prefix'] . $uid);
  82. }
  83. /**
  84. * 清理部门下所有用户缓存
  85. */
  86. public static function clearUserInfoByDeptId($dept_id): bool
  87. {
  88. $cache = static::cacheConfig();
  89. if (is_array($dept_id)) {
  90. $tags = [];
  91. foreach ($dept_id as $id) {
  92. $tags[] = $cache['dept'] . $id;
  93. }
  94. } else {
  95. $tags = $cache['dept'] . $dept_id;
  96. }
  97. return Cache::tag($tags)->clear();
  98. }
  99. /**
  100. * 清理角色下所有用户缓存
  101. */
  102. public static function clearUserInfoByRoleId($role_id): bool
  103. {
  104. $cache = static::cacheConfig();
  105. if (is_array($role_id)) {
  106. $tags = [];
  107. foreach ($role_id as $id) {
  108. $tags[] = $cache['role'] . $id;
  109. }
  110. } else {
  111. $tags = $cache['role'] . $role_id;
  112. }
  113. return Cache::tag($tags)->clear();
  114. }
  115. /**
  116. * 清理岗位下所有用户缓存
  117. */
  118. public static function clearUserInfoByPostId($post_id): bool
  119. {
  120. $cache = static::cacheConfig();
  121. if (is_array($post_id)) {
  122. $tags = [];
  123. foreach ($post_id as $id) {
  124. $tags[] = $cache['post'] . $id;
  125. }
  126. } else {
  127. $tags = $cache['post'] . $post_id;
  128. }
  129. return Cache::tag($tags)->clear();
  130. }
  131. }