package public import ( "app.yhyue.com/moapp/jybase/redis" "fmt" "sync" ) func PageNumParse(pageNum, pageSize, maxNum int64) (num, size int64, err error) { if pageNum < 1 { pageNum = 1 } if pageSize < 1 { pageSize = 1 } if maxNum > 0 && pageNum*pageSize > maxNum { err = fmt.Errorf("超出检索限制") } num = pageNum size = pageSize return } func PageRange(num, min, max int64) int64 { if num < min { return min } if num > max { return max } return num } var ( PLock *DocBuyLock ) type DocBuyLock struct { sync.Mutex UserLock map[string]*sync.Mutex } func NewDocBuyLock() *DocBuyLock { return &DocBuyLock{ UserLock: make(map[string]*sync.Mutex), } } func init() { PLock = NewDocBuyLock() } func GetNewDocBuyLock(str string) *sync.Mutex { PLock.Lock() if PLock.UserLock[str] == nil { PLock.UserLock[str] = &sync.Mutex{} } PLock.Unlock() return PLock.UserLock[str] } func RequestValidation(positionId int64) (err error) { repeatKey := fmt.Sprintf(RepeatKey, positionId) if ok, redisErr := redis.Exists(RedisCode, repeatKey); ok && redisErr == nil { err = fmt.Errorf("请求频繁 稍后再试") return } //方式重复性请求--1秒内 允许请求一次 redis.Put("other", repeatKey, "REPEAT", 1) return }