fix: redis issue in user management

This commit is contained in:
Feng Yue
2025-08-13 15:33:25 +08:00
parent 1ad720304c
commit 39c6e3146c
2 changed files with 36 additions and 7 deletions

View File

@@ -1294,6 +1294,32 @@ class RedisClient {
return 0 return 0
} }
} }
// 🔧 Basic Redis operations wrapper methods for convenience
async get(key) {
const client = this.getClientSafe()
return await client.get(key)
}
async set(key, value, ...args) {
const client = this.getClientSafe()
return await client.set(key, value, ...args)
}
async setex(key, ttl, value) {
const client = this.getClientSafe()
return await client.setex(key, ttl, value)
}
async del(...keys) {
const client = this.getClientSafe()
return await client.del(...keys)
}
async keys(pattern) {
const client = this.getClientSafe()
return await client.keys(pattern)
}
} }
const redisClient = new RedisClient() const redisClient = new RedisClient()

View File

@@ -112,13 +112,14 @@ class UserService {
// 📋 获取所有用户列表(管理员功能) // 📋 获取所有用户列表(管理员功能)
async getAllUsers(options = {}) { async getAllUsers(options = {}) {
try { try {
const client = redis.getClientSafe()
const { page = 1, limit = 20, role, isActive } = options const { page = 1, limit = 20, role, isActive } = options
const pattern = `${this.userPrefix}*` const pattern = `${this.userPrefix}*`
const keys = await redis.keys(pattern) const keys = await client.keys(pattern)
const users = [] const users = []
for (const key of keys) { for (const key of keys) {
const userData = await redis.get(key) const userData = await client.get(key)
if (userData) { if (userData) {
const user = JSON.parse(userData) const user = JSON.parse(userData)
@@ -308,15 +309,16 @@ class UserService {
// 🚫 使用户所有会话失效 // 🚫 使用户所有会话失效
async invalidateUserSessions(userId) { async invalidateUserSessions(userId) {
try { try {
const client = redis.getClientSafe()
const pattern = `${this.userSessionPrefix}*` const pattern = `${this.userSessionPrefix}*`
const keys = await redis.keys(pattern) const keys = await client.keys(pattern)
for (const key of keys) { for (const key of keys) {
const sessionData = await redis.get(key) const sessionData = await client.get(key)
if (sessionData) { if (sessionData) {
const session = JSON.parse(sessionData) const session = JSON.parse(sessionData)
if (session.userId === userId) { if (session.userId === userId) {
await redis.del(key) await client.del(key)
} }
} }
} }
@@ -356,8 +358,9 @@ class UserService {
// 📊 获取用户统计信息 // 📊 获取用户统计信息
async getUserStats() { async getUserStats() {
try { try {
const client = redis.getClientSafe()
const pattern = `${this.userPrefix}*` const pattern = `${this.userPrefix}*`
const keys = await redis.keys(pattern) const keys = await client.keys(pattern)
const stats = { const stats = {
totalUsers: 0, totalUsers: 0,
@@ -374,7 +377,7 @@ class UserService {
} }
for (const key of keys) { for (const key of keys) {
const userData = await redis.get(key) const userData = await client.get(key)
if (userData) { if (userData) {
const user = JSON.parse(userData) const user = JSON.parse(userData)
stats.totalUsers++ stats.totalUsers++