refactor: use redis hash to store online count

This commit is contained in:
Innei
2022-03-13 20:48:14 +08:00
parent f408129be9
commit d42b60eed1
2 changed files with 13 additions and 10 deletions

View File

@@ -349,9 +349,10 @@ export class AggregateService {
])
const [todayMaxOnline, todayOnlineTotal] = await Promise.all([
redisClient.get(getRedisKey(RedisKeys.MaxOnlineCount, dateFormat)),
redisClient.get(
getRedisKey(RedisKeys.MaxOnlineCount, dateFormat, 'total'),
redisClient.hget(getRedisKey(RedisKeys.MaxOnlineCount), dateFormat),
redisClient.hget(
getRedisKey(RedisKeys.MaxOnlineCount, 'total'),
dateFormat,
),
])

View File

@@ -69,16 +69,18 @@ export class WebEventsGateway
// get and store max_online_count
const maxOnlineCount =
+(await redisClient.get(
getRedisKey(RedisKeys.MaxOnlineCount, dateFormat),
+(await redisClient.hget(
getRedisKey(RedisKeys.MaxOnlineCount),
dateFormat,
)) || 0
await redisClient.set(
getRedisKey(RedisKeys.MaxOnlineCount, dateFormat),
await redisClient.hset(
getRedisKey(RedisKeys.MaxOnlineCount),
dateFormat,
Math.max(maxOnlineCount, await this.getcurrentClientCount()),
)
const key = getRedisKey(RedisKeys.MaxOnlineCount, dateFormat) + '_total'
const totalCount = +(await redisClient.get(key)) || 0
await redisClient.set(key, totalCount + 1)
const key = getRedisKey(RedisKeys.MaxOnlineCount, 'total')
const totalCount = +(await redisClient.hget(key, dateFormat)) || 0
await redisClient.hset(key, dateFormat, totalCount + 1)
})
super.handleConnect(socket)