This commit is contained in:
2026-03-17 19:51:47 +01:00
parent 84c9fa2b60
commit e496cb0017
5 changed files with 42 additions and 19 deletions
+8 -15
View File
@@ -7,32 +7,27 @@ import (
var (
mu sync.RWMutex
clients = make(map[uint32]Client)
ChatGroups = make(map[uint32]ChatGroup)
ClientsMap = make(map[uint32]map[uint32]*Client)
clients = make(map[uint32]*Client)
chatGroups = make(map[uint32]ChatGroup)
)
func AddGroupToCache(chatGroup *ChatGroup) {
mu.Lock()
defer mu.Unlock()
ChatGroups[chatGroup.Id] = *chatGroup
ClientsMap[chatGroup.Id] = make(map[uint32]*Client)
chatGroups[chatGroup.Id] = *chatGroup
}
func RemoveGroupFromCache(chatGroup *ChatGroup) {
mu.Lock()
defer mu.Unlock()
delete(ChatGroups, chatGroup.Id)
delete(ClientsMap, chatGroup.Id)
delete(chatGroups, chatGroup.Id)
}
func AddClientConnectionsToCache(client *Client) {
mu.Lock()
defer mu.Unlock()
for _, groupIn := range client.Groups {
if clients, ok := ClientsMap[groupIn.Id]; ok {
clients[client.Id] = client
}
chatGroups[groupIn.Id].Members[client.Id] = client
}
}
@@ -40,16 +35,14 @@ func RemoveClientConnectionsToCache(client *Client) {
mu.Lock()
defer mu.Unlock()
for _, groupIn := range client.Groups {
if clients, ok := ClientsMap[groupIn.Id]; ok {
delete(clients, client.Id)
}
delete(chatGroups[groupIn.Id].Members, client.Id)
}
}
func GetClientFromId(id uint32) (*Client, error) {
client, ok := &clients[id]
client, ok := clients[id]
if !ok {
return nil, errors.New("No such user")
return nil, errors.New("no such user")
}
return client, nil
}