1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- package util
- import (
- "app.yhyue.com/moapp/jybase/redis"
- "github.com/gogf/gf/v2/util/gconv"
- )
- type ReqLimit struct {
- Key string
- Size int
- }
- func (r *ReqLimit) Init() {
- if ok, err := redis.Exists("other", r.Key); err != nil || ok {
- return
- }
- for i := 0; i < r.Size; i++ {
- redis.RPUSH("other", r.Key, 1)
- }
- }
- func (r *ReqLimit) Clear() {
- redis.Del("other", r.Key)
- }
- // 查询是否被限流
- func (r *ReqLimit) IsLimit() bool {
- if r.Size <= 0 {
- return false
- }
- return redis.LLEN("other", r.Key) == 0
- }
- // 并发数限制
- // return true 被限制 false 正常
- func (r *ReqLimit) Limit() bool {
- if r.Size <= 0 {
- return false
- }
- return gconv.Int64(redis.LPOP("other", r.Key)) <= 0
- }
- func (r *ReqLimit) UnLimit() {
- if r.Size <= 0 {
- return
- }
- if int(redis.LLEN("other", r.Key)) < r.Size {
- redis.RPUSH("other", r.Key, 1)
- }
- }
|