package main import ( "fmt" "sync" ) var ( CacheUsers = make(map[uint32]*User) mu sync.RWMutex 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 CacheGetIdByName(name string) (uint32, error) { user, err := CacheGetUserByName(name) if err != nil { return 0, err } return user.Id, 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 }