bilibili-backup/app/service/bbq/user/internal/model/model.go
2019-04-22 02:59:20 +00:00

54 lines
1.4 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package model
import (
"encoding/json"
"go-common/library/ecode"
xtime "go-common/library/time"
"time"
)
const (
// MaxInt64 用于最大int64
MaxInt64 = int64(^uint64(0) >> 1)
// UserListLen 空间长度
UserListLen = 20
)
// Cache
const (
CacheKeyUserBase = "user_base:%d" //用户基本信息缓存key
CacheExpireUserBase = 600
)
// CursorValue 用于cursor的定位这里可以当做通用结构使用使用者自己根据需求定义cursor_id的含义
type CursorValue struct {
CursorID int64 `json:"cursor_id"`
CursorTime xtime.Time `json:"cursor_time"`
}
// ParseCursor 从cursor_prev和cursor_next判断请求的方向以及生成cursor
func ParseCursor(cursorPrev string, cursorNext string) (cursor CursorValue, directionNext bool, err error) {
// 判断是向前还是向后查询
directionNext = true
cursorStr := cursorNext
if len(cursorNext) == 0 && len(cursorPrev) > 0 {
directionNext = false
cursorStr = cursorPrev
}
// 解析cursor中的cursor_id
if len(cursorStr) != 0 {
var cursorData = []byte(cursorStr)
err = json.Unmarshal(cursorData, &cursor)
if err != nil {
err = ecode.ReqParamErr
return
}
}
// 第一次请求的时候携带的svid=0需要转成max传给dao层
if directionNext && cursor.CursorID == 0 {
cursor.CursorID = MaxInt64
cursor.CursorTime = xtime.Time(time.Now().Unix())
}
return
}