group.go 2.64 KB
Newer Older
hujiebin's avatar
hujiebin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
package group_c

import (
	"context"
	"git.hilo.cn/hilo-common/resource/redisCli"
	"hilo-group/_const/redis_key"
	"strings"
	"time"
)

func GetGroupMemberCount(groupId string) (int, error) {
	key := redis_key.GetGroupMemCountKey(groupId)
	return redisCli.RedisClient.Get(context.Background(), key).Int()
}

func SetGroupMemberCount(groupId string, count uint, ttl time.Duration) error {
	key := redis_key.GetGroupMemCountKey(groupId)
	return redisCli.RedisClient.Set(context.Background(), key, count, ttl).Err()
}

func ClearGroupMemberCount(groupId string) error {
	key := redis_key.GetGroupMemCountKey(groupId)
	return redisCli.RedisClient.Del(context.Background(), key).Err()
}

func getGroupMemberKey(groupId string) string {
	return strings.Replace(redis_key.GroupMemberPrefix, "{groupId}", groupId, -1)
}

func getGroupConsumeKey(groupId string) string {
	return strings.Replace(redis_key.GroupConsumePrefix, "{groupId}", groupId, -1)
}

func GetGroupMember(groupId string) ([]string, error) {
	key := getGroupMemberKey(groupId)
	return redisCli.RedisClient.SMembers(context.Background(), key).Result()
}

func GetGroupMemberCard(groupId string) (int64, error) {
	key := getGroupMemberKey(groupId)
	return redisCli.RedisClient.SCard(context.Background(), key).Result()
}

func GetGroupMemberAsMap(groupId string) (map[string]struct{}, error) {
	key := getGroupMemberKey(groupId)
	return redisCli.RedisClient.SMembersMap(context.Background(), key).Result()
}

func SetExists(groupId string) (int64, error) {
	key := getGroupMemberKey(groupId)
	return redisCli.RedisClient.Exists(context.Background(), key).Result()
}

func AddGroupMember(groupId string, extIds []string) (int64, error) {
	key := getGroupMemberKey(groupId)
	return redisCli.RedisClient.SAdd(context.Background(), key, extIds).Result()
}

func RemoveGroupMember(groupId string, externalId string) (int64, error) {
	key := getGroupMemberKey(groupId)
	return redisCli.RedisClient.SRem(context.Background(), key, externalId).Result()
}

func SetGroupMemberTTL(groupId string, ttl time.Duration) (bool, error) {
	key := getGroupMemberKey(groupId)
	return redisCli.RedisClient.Expire(context.Background(), key, ttl).Result()
}

func GetGroupConsume(groupId string) (uint64, error) {
	key := getGroupConsumeKey(groupId)
	return redisCli.RedisClient.Get(context.Background(), key).Uint64()
}

func SetGroupConsume(groupId string, consume uint64, ttl time.Duration) error {
	key := getGroupConsumeKey(groupId)
	return redisCli.RedisClient.Set(context.Background(), key, consume, ttl).Err()
}

func ClearGroupConsume(groupId string) error {
	key := getGroupConsumeKey(groupId)
	return redisCli.RedisClient.Del(context.Background(), key).Err()
}