groupBanned.go 1.32 KB
Newer Older
hujiebin's avatar
hujiebin committed
1 2 3 4 5
package group_m

import (
	"git.hilo.cn/hilo-common/domain"
	"git.hilo.cn/hilo-common/resource/mysql"
iamhujiebin's avatar
iamhujiebin committed
6 7
	"github.com/bluele/gcache"
	"time"
hujiebin's avatar
hujiebin committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
)

type GroupBanned struct {
	mysql.Entity
	ImGroupId string
	MgrId     uint64
	RuleId    uint64
}

func (banned *GroupBanned) Set(model *domain.Model) error {
	return model.Db.Where(banned).Create(banned).Error
}

func (banned *GroupBanned) Get(model *domain.Model) error {
	return model.Db.Where(banned).First(banned).Error
}

func (banned *GroupBanned) Delete(model *domain.Model) error {
	return model.Db.Where(banned).Delete(&GroupBanned{}).Error
}

iamhujiebin's avatar
iamhujiebin committed
29 30
var bannedGroupCache = gcache.New(100).LRU().Build()

hujiebin's avatar
hujiebin committed
31
func GetBannedGroups(model *domain.Model) ([]GroupBanned, error) {
iamhujiebin's avatar
iamhujiebin committed
32 33 34 35 36
	key := "banned"
	if data, err := bannedGroupCache.Get(key); err == nil {
		model.Log.Infof("GetBannedGroups cache:%v", len(data.([]GroupBanned)))
		return data.([]GroupBanned), nil
	}
hujiebin's avatar
hujiebin committed
37 38 39 40 41
	result := make([]GroupBanned, 0)
	err := model.Db.Find(&result).Error
	if err != nil {
		return nil, err
	}
iamhujiebin's avatar
iamhujiebin committed
42
	bannedGroupCache.SetWithExpire(key, result, time.Minute*5)
hujiebin's avatar
hujiebin committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56
	return result, nil
}

func GetBannedGroupsMap(model *domain.Model) (map[string]uint64, error) {
	r, err := GetBannedGroups(model)
	if err != nil {
		return nil, err
	}
	result := make(map[string]uint64, 0)
	for _, i := range r {
		result[i.ImGroupId] = i.MgrId
	}
	return result, nil
}