rds.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package broadcast
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "git.jiaxianghudong.com/go/logs"
  6. "git.jiaxianghudong.com/webs/pkg/rds"
  7. "time"
  8. )
  9. type ProblemInfo struct {
  10. ProblemId string `json:"problem_id"`
  11. Problem string `json:"problem"`
  12. AnswerList string `json:"answer_list"`
  13. Answer []string `json:"answer"`
  14. Code string `json:"code"`
  15. EndTime int64 `json:"end_time"`
  16. AttendLimit int `json:"attend_limit"`
  17. Bean int `json:"bean"`
  18. IsEnd bool `json:"is_end"`
  19. QuestionLimit int `json:"question_limit"`
  20. }
  21. func (this *ProblemInfo) GetData(userId string) error {
  22. data := rds.Redis.Get(fmt.Sprintf(PROBLEM, userId)).Val()
  23. if data != "" {
  24. err := json.Unmarshal([]byte(data), this)
  25. //fmt.Println(data)
  26. if err != nil {
  27. logs.Errorf("broadcast GetData json err:%v", userId, err)
  28. }
  29. json.Unmarshal([]byte(this.AnswerList), &this.Answer)
  30. } else {
  31. return fmt.Errorf("不能为空")
  32. }
  33. return nil
  34. }
  35. func (this *ProblemInfo) SetData(userId string) {
  36. data, _ := json.Marshal(this)
  37. rds.Redis.Set(fmt.Sprintf(PROBLEM, userId), string(data), 24*time.Hour).Val()
  38. }