Files
go-socket/cache.go
T

77 lines
1.2 KiB
Go

package main
import (
"fmt"
"sync"
)
var (
CacheClients = make(map[uint32]*Client)
mu sync.RWMutex
Groups = make(map[uint32]*Group)
)
func CacheGetClientById(id uint32) (*Client, error) {
mu.RLock()
defer mu.RUnlock()
client, ok := CacheClients[id]
if !ok {
return nil, fmt.Errorf("client %d not found", id)
}
return client, nil
}
func CacheGetIdByName(name string) (uint32, error) {
client, err := CacheGetClientByName(name)
if err != nil {
return 0, err
}
return client.Id, nil
}
func CacheGetClientByName(name string) (*Client, error) {
mu.RLock()
defer mu.RUnlock()
for _, client := range CacheClients {
if client.Name == name {
return client, nil
}
}
return nil, fmt.Errorf("client %s not found", name)
}
func CacheSaveClient(client *Client) {
mu.Lock()
defer mu.Unlock()
CacheClients[client.Id] = client
}
func CacheDeleteClient(id uint32) {
mu.Lock()
defer mu.Unlock()
delete(CacheClients, id)
}
func CacheSaveGroup(group *Group) {
mu.Lock()
defer mu.Unlock()
Groups[group.Id] = group
}
func CacheGetGroup(id uint32) (*Group, error) {
mu.RLock()
defer mu.RUnlock()
group, ok := Groups[id]
if !ok {
return nil, fmt.Errorf("group %d not found", id)
}
return group, nil
}