You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
71 lines
1.5 KiB
71 lines
1.5 KiB
2 years ago
|
package msggateway
|
||
2 years ago
|
|
||
|
import "sync"
|
||
|
|
||
|
type UserMap struct {
|
||
|
m sync.Map
|
||
|
}
|
||
|
|
||
|
func newUserMap() *UserMap {
|
||
|
return &UserMap{}
|
||
|
}
|
||
2 years ago
|
func (u *UserMap) GetAll(key string) ([]*Client, bool) {
|
||
2 years ago
|
allClients, ok := u.m.Load(key)
|
||
|
if ok {
|
||
2 years ago
|
return allClients.([]*Client), ok
|
||
2 years ago
|
}
|
||
2 years ago
|
return nil, ok
|
||
2 years ago
|
}
|
||
2 years ago
|
func (u *UserMap) Get(key string, platformID int) ([]*Client, bool, bool) {
|
||
2 years ago
|
allClients, userExisted := u.m.Load(key)
|
||
|
if userExisted {
|
||
2 years ago
|
var clients []*Client
|
||
2 years ago
|
for _, client := range allClients.([]*Client) {
|
||
2 years ago
|
if client.platformID == platformID {
|
||
2 years ago
|
clients = append(clients, client)
|
||
2 years ago
|
}
|
||
|
}
|
||
2 years ago
|
if len(clients) > 0 {
|
||
|
return clients, userExisted, true
|
||
|
|
||
|
}
|
||
|
return clients, userExisted, false
|
||
2 years ago
|
}
|
||
2 years ago
|
return nil, userExisted, false
|
||
2 years ago
|
}
|
||
|
func (u *UserMap) Set(key string, v *Client) {
|
||
|
allClients, existed := u.m.Load(key)
|
||
|
if existed {
|
||
|
oldClients := allClients.([]*Client)
|
||
|
oldClients = append(oldClients, v)
|
||
|
u.m.Store(key, oldClients)
|
||
|
} else {
|
||
2 years ago
|
var clients []*Client
|
||
2 years ago
|
clients = append(clients, v)
|
||
|
u.m.Store(key, clients)
|
||
|
}
|
||
|
}
|
||
2 years ago
|
func (u *UserMap) delete(key string, platformID int) (isDeleteUser bool) {
|
||
2 years ago
|
allClients, existed := u.m.Load(key)
|
||
|
if existed {
|
||
|
oldClients := allClients.([]*Client)
|
||
2 years ago
|
var a []*Client
|
||
2 years ago
|
for _, client := range oldClients {
|
||
2 years ago
|
if client.platformID != platformID {
|
||
2 years ago
|
a = append(a, client)
|
||
|
}
|
||
|
}
|
||
|
if len(a) == 0 {
|
||
|
u.m.Delete(key)
|
||
2 years ago
|
return true
|
||
2 years ago
|
} else {
|
||
|
u.m.Store(key, a)
|
||
2 years ago
|
return false
|
||
2 years ago
|
}
|
||
|
}
|
||
2 years ago
|
return existed
|
||
2 years ago
|
}
|
||
|
func (u *UserMap) DeleteAll(key string) {
|
||
|
u.m.Delete(key)
|
||
|
}
|