model.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package model
  2. import (
  3. "encoding/json"
  4. "go-common/library/ecode"
  5. xtime "go-common/library/time"
  6. "time"
  7. )
  8. const (
  9. // MaxInt64 用于最大int64
  10. MaxInt64 = int64(^uint64(0) >> 1)
  11. // UserListLen 空间长度
  12. UserListLen = 20
  13. )
  14. // Cache
  15. const (
  16. CacheKeyUserBase = "user_base:%d" //用户基本信息缓存key
  17. CacheExpireUserBase = 600
  18. )
  19. // CursorValue 用于cursor的定位,这里可以当做通用结构使用,使用者自己根据需求定义cursor_id的含义
  20. type CursorValue struct {
  21. CursorID int64 `json:"cursor_id"`
  22. CursorTime xtime.Time `json:"cursor_time"`
  23. }
  24. // ParseCursor 从cursor_prev和cursor_next,判断请求的方向,以及生成cursor
  25. func ParseCursor(cursorPrev string, cursorNext string) (cursor CursorValue, directionNext bool, err error) {
  26. // 判断是向前还是向后查询
  27. directionNext = true
  28. cursorStr := cursorNext
  29. if len(cursorNext) == 0 && len(cursorPrev) > 0 {
  30. directionNext = false
  31. cursorStr = cursorPrev
  32. }
  33. // 解析cursor中的cursor_id
  34. if len(cursorStr) != 0 {
  35. var cursorData = []byte(cursorStr)
  36. err = json.Unmarshal(cursorData, &cursor)
  37. if err != nil {
  38. err = ecode.ReqParamErr
  39. return
  40. }
  41. }
  42. // 第一次请求的时候,携带的svid=0,需要转成max传给dao层
  43. if directionNext && cursor.CursorID == 0 {
  44. cursor.CursorID = MaxInt64
  45. cursor.CursorTime = xtime.Time(time.Now().Unix())
  46. }
  47. return
  48. }