Files
go-socket/cache.go
T
2026-04-05 22:38:02 +02:00

69 lines
1.0 KiB
Go

package main
import (
"fmt"
"sync"
)
var (
mu sync.RWMutex
CacheUsers = make(map[uint32]*User)
Groups = make(map[uint32]*Group)
)
func CacheGetUserById(id uint32) (*User, error) {
mu.RLock()
defer mu.RUnlock()
user, ok := CacheUsers[id]
if !ok {
return nil, fmt.Errorf("user %d not found", id)
}
return user, nil
}
func CacheGetUserByName(name string) (*User, error) {
mu.RLock()
defer mu.RUnlock()
for _, user := range CacheUsers {
if user.Name == name {
return user, nil
}
}
return nil, fmt.Errorf("user %s not found", name)
}
func CacheSaveUser(user *User) {
mu.Lock()
defer mu.Unlock()
CacheUsers[user.Id] = user
}
func CacheDeleteUser(id uint32) {
mu.Lock()
defer mu.Unlock()
delete(CacheUsers, 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
}