123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- 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() {
- r.Clear()
- 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)
- }
- }
|