63 lines
1.8 KiB
Go
63 lines
1.8 KiB
Go
package cachekey
|
|
|
|
import (
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
const (
|
|
onlineKeyBase = "ONLINE:"
|
|
onlineChannelBase = "online_change"
|
|
OnlineExpire = time.Hour / 2
|
|
// OnlineUserCountKey 在线人数缓存键
|
|
onlineUserCountKeyBase = "ONLINE_USER_COUNT"
|
|
// OnlineUserCountHistoryKey 在线人数历史缓存键
|
|
onlineUserCountHistoryKeyBase = "ONLINE_USER_COUNT_HISTORY"
|
|
// OnlineUserCountHistoryRetention 在线人数历史保留时长
|
|
OnlineUserCountHistoryRetention = 60 * 24 * time.Hour
|
|
)
|
|
|
|
var (
|
|
OnlineKey = onlineKeyBase
|
|
OnlineChannel = onlineChannelBase
|
|
OnlineUserCountKey = onlineUserCountKeyBase
|
|
OnlineUserCountHistoryKey = onlineUserCountHistoryKeyBase
|
|
)
|
|
|
|
func SetOnlinePrefix(prefix string, useHashTag bool, redisMode string) {
|
|
normalized := normalizeOnlinePrefix(prefix, useHashTag, redisMode)
|
|
if normalized == "" {
|
|
OnlineKey = onlineKeyBase
|
|
OnlineChannel = onlineChannelBase
|
|
OnlineUserCountKey = onlineUserCountKeyBase
|
|
OnlineUserCountHistoryKey = onlineUserCountHistoryKeyBase
|
|
return
|
|
}
|
|
OnlineKey = normalized + onlineKeyBase
|
|
OnlineChannel = normalized + onlineChannelBase
|
|
OnlineUserCountKey = normalized + onlineUserCountKeyBase
|
|
OnlineUserCountHistoryKey = normalized + onlineUserCountHistoryKeyBase
|
|
}
|
|
|
|
func normalizeOnlinePrefix(prefix string, useHashTag bool, redisMode string) string {
|
|
prefix = strings.TrimSpace(prefix)
|
|
if prefix == "" {
|
|
return ""
|
|
}
|
|
prefix = strings.TrimSuffix(prefix, ":")
|
|
if useHashTag || strings.EqualFold(redisMode, "cluster") {
|
|
if !(strings.HasPrefix(prefix, "{") && strings.HasSuffix(prefix, "}")) {
|
|
prefix = "{" + prefix + "}"
|
|
}
|
|
}
|
|
return prefix + ":"
|
|
}
|
|
|
|
func GetOnlineKey(userID string) string {
|
|
return OnlineKey + userID
|
|
}
|
|
|
|
func GetOnlineKeyUserID(key string) string {
|
|
return strings.TrimPrefix(key, OnlineKey)
|
|
}
|