Browse Source

feat: 添加在线用户数

runphp 1 month ago
parent
commit
855da0efda
1 changed files with 11 additions and 0 deletions
  1. 11 0
      src/Hook/AuthHook.php

+ 11 - 0
src/Hook/AuthHook.php

@@ -6,6 +6,7 @@ namespace SixShop\Auth\Hook;
 
 use Firebase\JWT\ExpiredException;
 use SixShop\Auth\Auth;
+use SixShop\Auth\Enum\UserTypeEnum;
 use SixShop\Core\Attribute\Hook;
 use think\Cache;
 
@@ -66,4 +67,14 @@ class AuthHook
             }
         }
     }
+
+    /**
+     * 获取某用户类型的在线用户数
+     */
+    public function getOnlineCount(UserTypeEnum $userType): int
+    {
+        $pattern = $this->cache->getCacheKey(self::TOKEN_INFO . $userType->value . ':*');
+        $keys = $this->cache->handler()->keys($pattern);
+        return count($keys);
+    }
 }