package main import ( "errors" "sync" ) var ( mu sync.RWMutex clients = make(map[uint32]Client) ChatGroups = make(map[uint32]ChatGroup) 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 AddClientConnectionsToCache(client *Client) { mu.Lock() defer mu.Unlock() for _, groupIn := range client.Groups { if clients, ok := ClientsMap[groupIn.Id]; ok { clients[client.Id] = client } } } 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) } } } func GetClientFromId(id uint32) (*Client, error) { client, ok := &clients[id] if !ok { return nil, errors.New("No such user") } return client, nil }