add cache for groups and clients, http sending message remains to be add

This commit is contained in:
2026-03-15 14:15:24 +01:00
parent 76fbb8b970
commit c97b21a39e
7 changed files with 166 additions and 89 deletions
+57
View File
@@ -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)
}
}