package manager import ( "context" "github.com/go-redis/redis/v8" "log" "strconv" ) const ( terminal_key = "terminal" ) type TerminalManager struct { Ctx context.Context RedisClient *redis.Client } func (m *TerminalManager) GetTerminal(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, terminal_key, field).Result() if err != nil { log.Printf("HGet error %s\n", err.Error()) return nil } return &result } func (m *TerminalManager) SetTerminal(uid uint64, value string) error { field := strconv.FormatUint(uid, 10) result, err := m.RedisClient.HSet(m.Ctx, terminal_key, field, value).Result() if err != nil { log.Printf("HSet error %s\n") } else { log.Printf("HSet %s:%s result: %d\n", terminal_key, field, result) } return err } func (m *TerminalManager) RemoveTerminal(uid uint64) error { field := strconv.FormatUint(uid, 10) result, err := m.RedisClient.HDel(m.Ctx, terminal_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 *TerminalManager) 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, terminal_key).Result() if err != nil { log.Printf("HGetAll error: %s\n", err.Error()) return nil } else { return &result } }