backend/pkg/member/domain/redis.go

57 lines
1.7 KiB
Go

package domain
import (
"fmt"
"strings"
)
// RedisKey represents a Redis key type with helper methods for key construction.
type RedisKey string
const (
// AccountRedisKey is the Redis key prefix for account data
AccountRedisKey RedisKey = "account"
// AccountUIDRedisKey is the Redis key prefix for account-UID binding data
AccountUIDRedisKey RedisKey = "account_uid"
// UserRedisKey is the Redis key prefix for user data
UserRedisKey RedisKey = "user"
// MemberPrefixRedisKey is the common prefix for all member-related Redis keys
MemberPrefixRedisKey = "member"
)
// ToString converts the RedisKey to its full string representation with the member prefix.
func (key RedisKey) ToString() string {
return "member:" + string(key)
}
// With appends additional parts to the RedisKey, separated by colons.
func (key RedisKey) With(s ...string) RedisKey {
parts := append([]string{string(key)}, s...)
return RedisKey(strings.Join(parts, ":"))
}
// GetAccountRedisKey generates a Redis key for account data by ID.
func GetAccountRedisKey(id string) string {
return AccountRedisKey.With(id).ToString()
}
// GetAccountUIDRedisKey generates a Redis key for account-UID binding data by ID.
func GetAccountUIDRedisKey(id string) string {
return AccountUIDRedisKey.With(id).ToString()
}
// GetUserRedisKey generates a Redis key for user data by ID.
func GetUserRedisKey(id string) string {
return UserRedisKey.With(id).ToString()
}
var (
// checkVerifyKey is the Redis key prefix for verification codes
checkVerifyKey = fmt.Sprintf("%s:verify:", MemberPrefixRedisKey)
)
// GetCheckVerifyKey generates a Redis key for verification code data.
func GetCheckVerifyKey(codeType, account string) string {
return fmt.Sprintf("%s%s:%s", checkVerifyKey, codeType, account)
}