gethistory.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/library/cache/redis"
  6. "go-common/library/log"
  7. )
  8. //GetHistoryData 获取历史数据
  9. func (d *Dao) GetHistoryData(ctx context.Context, roomid int64) (result map[string][]string, err error) {
  10. var conn = d.redis.Get(ctx)
  11. defer conn.Close()
  12. var admkey = fmt.Sprintf("%s%d", _adminMsgHistoryCache, roomid)
  13. var userkey = fmt.Sprintf("%s%d", _msgHistoryCache, roomid)
  14. if err = conn.Send("LRANGE", admkey, 0, 9); err != nil {
  15. log.Error("DM: LRANGE ADMIN KEY ROOMID %d ERR:%v", roomid, err)
  16. }
  17. if err = conn.Send("LRANGE", userkey, 0, 9); err != nil {
  18. log.Error("DM: LRANGE USER KEY ROOMID %d ERR:%v", roomid, err)
  19. }
  20. if err = conn.Flush(); err != nil {
  21. log.Error("DM: Flush KEY ROOMID %d ERR:%v", roomid, err)
  22. return nil, err
  23. }
  24. var admin, user [][]byte
  25. admin, err = redis.ByteSlices(conn.Receive())
  26. if err != nil {
  27. log.Error("DM: ByteSlices ADMIN KEY ROOMID %d ERR:%v", roomid, err)
  28. return nil, err
  29. }
  30. user, err = redis.ByteSlices(conn.Receive())
  31. if err != nil {
  32. log.Error("DM: ByteSlices USER KEY ROOMID %d ERR:%v", roomid, err)
  33. return nil, err
  34. }
  35. result = make(map[string][]string)
  36. result["admin"] = make([]string, 0, 10)
  37. result["room"] = make([]string, 0, 10)
  38. for i := len(admin) - 1; i >= 0; i-- {
  39. result["admin"] = append(result["admin"], string(admin[i]))
  40. }
  41. for i := len(user) - 1; i >= 0; i-- {
  42. result["room"] = append(result["room"], string(user[i]))
  43. }
  44. return result, nil
  45. }