add cache for groups and clients, http sending message remains to be add
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var Groups map[uint32]ChatGroup
|
||||
var ConnectedClients map[uint32]map[*Client]struct{}
|
||||
|
||||
func InitCache() {
|
||||
groups, err := GetAllChatGroups(context.Background())
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
for _, group := range groups {
|
||||
Groups[group.Id] = group
|
||||
}
|
||||
}
|
||||
|
||||
func GetGroupById(groupId uint32) (*ChatGroup, error) {
|
||||
group, ok := Groups[groupId]
|
||||
if !ok {
|
||||
return nil, errors.New("group not found")
|
||||
}
|
||||
return &group, nil
|
||||
}
|
||||
|
||||
func AddOrUpdateGroupToCache(mu *sync.Mutex, group ChatGroup) {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
Groups[group.Id] = group
|
||||
}
|
||||
|
||||
func RemoveGroupFromCache(mu *sync.Mutex, groupId uint32) {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
delete(Groups, groupId)
|
||||
}
|
||||
|
||||
func AddOrUpdateConnectedClientToCache(mu *sync.Mutex, client *Client) {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
for _, groupId := range client.User.MemberGroupsId {
|
||||
ConnectedClients[groupId][client] = struct{}{}
|
||||
}
|
||||
}
|
||||
|
||||
func RemoveConnectedClientFromCache(mu *sync.Mutex, client *Client) {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
for _, groupId := range client.User.MemberGroupsId {
|
||||
delete(ConnectedClients[groupId], client)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user