util.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package public
  2. import (
  3. "app.yhyue.com/moapp/jybase/redis"
  4. "fmt"
  5. "sync"
  6. )
  7. func PageNumParse(pageNum, pageSize, maxNum int64) (num, size int64, err error) {
  8. if pageNum < 1 {
  9. pageNum = 1
  10. }
  11. if pageSize < 1 {
  12. pageSize = 1
  13. }
  14. if maxNum > 0 && pageNum*pageSize > maxNum {
  15. err = fmt.Errorf("超出检索限制")
  16. }
  17. num = pageNum
  18. size = pageSize
  19. return
  20. }
  21. func PageRange(num, min, max int64) int64 {
  22. if num < min {
  23. return min
  24. }
  25. if num > max {
  26. return max
  27. }
  28. return num
  29. }
  30. var (
  31. PLock *DocBuyLock
  32. )
  33. type DocBuyLock struct {
  34. sync.Mutex
  35. UserLock map[string]*sync.Mutex
  36. }
  37. func NewDocBuyLock() *DocBuyLock {
  38. return &DocBuyLock{
  39. UserLock: make(map[string]*sync.Mutex),
  40. }
  41. }
  42. func init() {
  43. PLock = NewDocBuyLock()
  44. }
  45. func GetNewDocBuyLock(str string) *sync.Mutex {
  46. PLock.Lock()
  47. if PLock.UserLock[str] == nil {
  48. PLock.UserLock[str] = &sync.Mutex{}
  49. }
  50. PLock.Unlock()
  51. return PLock.UserLock[str]
  52. }
  53. func RequestValidation(positionId int64) (err error) {
  54. repeatKey := fmt.Sprintf(RepeatKey, positionId)
  55. if ok, redisErr := redis.Exists(RedisCode, repeatKey); ok && redisErr == nil {
  56. err = fmt.Errorf("请求频繁 稍后再试")
  57. return
  58. }
  59. //方式重复性请求--1秒内 允许请求一次
  60. redis.Put("other", repeatKey, "REPEAT", 1)
  61. return
  62. }