package manager import ( "context" "github.com/go-redis/redis/v8" "log" "strconv" ) const ( user_key = "user" ) type UserManager struct { Ctx context.Context RedisClient *redis.Client } func (m *UserManager) GetUser(uid uint64) *string { field := strconv.FormatUint(uid, 10) //ctx, _ := context.WithTimeout(m.Ctx, time.Millisecond*500) //result := m.RedisClient.Get(ctx, field) result, err := m.RedisClient.HGet(m.Ctx, user_key, field).Result() if err != nil { log.Printf("HGet error %s\n", err.Error()) return nil } return &result } func (m *UserManager) AddUser(uid uint64, value string) error { field := strconv.FormatUint(uid, 10) result, err := m.RedisClient.HSet(m.Ctx, user_key, field, value).Result() if err != nil { log.Printf("HSet error %s\n") } else { log.Printf("HSet %s:%s result: %d\n", user_key, field, result) } return err } func (m *UserManager) RemoveUser(uid uint64) error { field := strconv.FormatUint(uid, 10) result, err := m.RedisClient.HDel(m.Ctx, user_key, field).Result() if err != nil { log.Printf("HDel error: %s\n", err.Error()) } else { log.Printf("HDel %s return %d\n", field, result) } return err } func (m *UserManager) GetAll() *map[string]string { //ctx, _ := context.WithTimeout(m.Ctx, time.Millisecond*500) //result := m.RedisClient.Get(ctx, field) result, err := m.RedisClient.HGetAll(m.Ctx, user_key).Result() if err != nil { log.Printf("HGetAll error: %s\n", err.Error()) return nil } else { return &result } }