Files
go-socket/cache.go
T
GitProtogen 66e189d247 add cache
2026-03-16 13:50:22 +01:00

45 lines
916 B
Go

package main
import "sync"
var (
mu sync.RWMutex
ChatGroups = make(map[uint32]ChatGroup)
Clients = make(map[uint32]Client)
ClientsMap = make(map[uint32]map[uint32]*Client)
)
func AddGroupToCache(chatGroup *ChatGroup) {
mu.Lock()
defer mu.Unlock()
ChatGroups[chatGroup.Id] = *chatGroup
ClientsMap[chatGroup.Id] = make(map[uint32]*Client)
}
func RemoveGroupFromCache(chatGroup *ChatGroup) {
mu.Lock()
defer mu.Unlock()
delete(ChatGroups, chatGroup.Id)
delete(ClientsMap, chatGroup.Id)
}
func AddUserToCache(client *Client) {
mu.Lock()
defer mu.Unlock()
for _, groupIn := range client.Groups {
if clients, ok := ClientsMap[groupIn.Id]; ok {
clients[client.Id] = client
}
}
}
func RemoveClientFromCache(client *Client) {
mu.Lock()
defer mu.Unlock()
for _, groupIn := range client.Groups {
if clients, ok := ClientsMap[groupIn.Id]; ok {
delete(clients, client.Id)
}
}
}