|
@@ -14,13 +14,15 @@ import (
|
|
|
var Lst *LimitSearchText
|
|
|
|
|
|
type LimitSearchText struct {
|
|
|
- TimeOut int
|
|
|
- Count int
|
|
|
- UserIds []string
|
|
|
- Flag bool
|
|
|
- TotalPage int
|
|
|
- Msg string
|
|
|
- Percentage int
|
|
|
+ TimeOut int
|
|
|
+ Count int
|
|
|
+ UserIds []string
|
|
|
+ Flag bool
|
|
|
+ TotalPage int
|
|
|
+ Msg string
|
|
|
+ Percentage int
|
|
|
+ LimitKey string
|
|
|
+ LimitKeyUser string
|
|
|
}
|
|
|
|
|
|
func IsSearchLimit(searchItems []string) bool {
|
|
@@ -36,13 +38,15 @@ func IsSearchLimit(searchItems []string) bool {
|
|
|
func InitLimitSearchText(flag bool) {
|
|
|
limitSearchConfig, _ := config.Sysconfig["limitSearchText"].(map[string]interface{})
|
|
|
Lst = &LimitSearchText{
|
|
|
- TimeOut: util.IntAllDef(limitSearchConfig["timeout"], 60),
|
|
|
- UserIds: util.ObjArrToStringArr(limitSearchConfig["userIds"].([]interface{})),
|
|
|
- Count: util.IntAllDef(limitSearchConfig["count"], 3),
|
|
|
- TotalPage: util.IntAllDef(limitSearchConfig["totalPage"], 3),
|
|
|
- Flag: limitSearchConfig["flag"].(bool),
|
|
|
- Msg: limitSearchConfig["msg"].(string),
|
|
|
- Percentage: util.IntAllDef(limitSearchConfig["percentage"], 80),
|
|
|
+ TimeOut: util.IntAllDef(limitSearchConfig["timeout"], 60),
|
|
|
+ UserIds: util.ObjArrToStringArr(limitSearchConfig["userIds"].([]interface{})),
|
|
|
+ Count: util.IntAllDef(limitSearchConfig["count"], 3),
|
|
|
+ TotalPage: util.IntAllDef(limitSearchConfig["totalPage"], 3),
|
|
|
+ Flag: limitSearchConfig["flag"].(bool),
|
|
|
+ Msg: limitSearchConfig["msg"].(string),
|
|
|
+ Percentage: util.IntAllDef(limitSearchConfig["percentage"], 80),
|
|
|
+ LimitKey: fmt.Sprintf(util.ObjToString(limitSearchConfig["limitKey"]), "jy_limitSearchText"),
|
|
|
+ LimitKeyUser: fmt.Sprintf(util.ObjToString(limitSearchConfig["limitKey"]), "%s"),
|
|
|
}
|
|
|
if !Lst.Flag {
|
|
|
return
|
|
@@ -54,11 +58,11 @@ func InitLimitSearchText(flag bool) {
|
|
|
func (l *LimitSearchText) Init() {
|
|
|
l.Clear()
|
|
|
for i := 0; i < Lst.Count; i++ {
|
|
|
- redis.RPUSH("other", "jy_limitSearchText", 1)
|
|
|
+ redis.RPUSH("other", l.LimitKey, 1)
|
|
|
}
|
|
|
}
|
|
|
func (l *LimitSearchText) Clear() {
|
|
|
- redis.Del("other", "jy_limitSearchText")
|
|
|
+ redis.Del("other", l.LimitKey)
|
|
|
}
|
|
|
|
|
|
//限制正文查询
|
|
@@ -69,18 +73,18 @@ func (l *LimitSearchText) IsLimited(r *http.Request, w http.ResponseWriter, s *h
|
|
|
if !l.Flag {
|
|
|
return 1
|
|
|
}
|
|
|
- var llen = int(redis.LLEN("other", "jy_limitSearchText"))
|
|
|
+ var llen = int(redis.LLEN("other", l.LimitKey))
|
|
|
if l.TimeOut > 0 {
|
|
|
limitFlag, isNew := l.getFlag(r, w, s)
|
|
|
if isNew {
|
|
|
if llen <= l.Count/2 {
|
|
|
- timeLimit, _ := redis.Exists("other", "jy_limitSearchText_"+limitFlag)
|
|
|
+ timeLimit, _ := redis.Exists("other", fmt.Sprintf(l.LimitKeyUser, limitFlag))
|
|
|
if timeLimit {
|
|
|
return -1
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- redis.Put("other", "jy_limitSearchText_"+limitFlag, 1, l.TimeOut)
|
|
|
+ redis.Put("other", fmt.Sprintf(l.LimitKeyUser, limitFlag), 1, l.TimeOut)
|
|
|
}
|
|
|
if l.Count == -2 {
|
|
|
return 1
|
|
@@ -94,7 +98,7 @@ func (l *LimitSearchText) IsLimited(r *http.Request, w http.ResponseWriter, s *h
|
|
|
}
|
|
|
}
|
|
|
//
|
|
|
- pollLimit := redis.LPOP("other", "jy_limitSearchText")
|
|
|
+ pollLimit := redis.LPOP("other", l.LimitKey)
|
|
|
if util.IntAll(pollLimit) <= 0 {
|
|
|
return -2
|
|
|
}
|
|
@@ -104,8 +108,8 @@ func (l *LimitSearchText) Limit() {
|
|
|
if !l.Flag {
|
|
|
return
|
|
|
}
|
|
|
- if int(redis.LLEN("other", "jy_limitSearchText")) < l.Count {
|
|
|
- redis.RPUSH("other", "jy_limitSearchText", 1)
|
|
|
+ if int(redis.LLEN("other", l.LimitKey)) < l.Count {
|
|
|
+ redis.RPUSH("other", l.LimitKey, 1)
|
|
|
}
|
|
|
}
|
|
|
func (l *LimitSearchText) getFlag(r *http.Request, w http.ResponseWriter, s *httpsession.Session) (string, bool) {
|