From 7f09d4c5c56d02ee48802cafa8463e3bb14efedd Mon Sep 17 00:00:00 2001 From: fy Date: Sat, 17 Aug 2024 15:46:13 +0800 Subject: [PATCH] =?UTF-8?q?imp:=20=E5=87=8F=E5=B0=91=E8=87=AA=E5=AE=9A?= =?UTF-8?q?=E4=B9=89=E5=9B=9E=E5=A4=8D=E5=BE=88=E5=A4=9A=E7=9A=84=E9=AA=B0?= =?UTF-8?q?=E5=AD=90=E7=9A=84=E5=86=85=E5=AD=98=E7=94=B3=E8=AF=B7=E9=87=8F?= =?UTF-8?q?=20(#1010)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dice/ext_reply_logic.go | 24 ++++++++++++++++++++++-- dice/im_session.go | 2 +- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/dice/ext_reply_logic.go b/dice/ext_reply_logic.go index 1131da47..c65314f3 100644 --- a/dice/ext_reply_logic.go +++ b/dice/ext_reply_logic.go @@ -61,6 +61,26 @@ func (m *ReplyConditionTextMatch) Clean() { m.Value = strings.TrimSpace(m.Value) } +type replyRegexCacheType struct { + cache SyncMap[string, *regexp.Regexp] +} + +func (r *replyRegexCacheType) compile(expr string) *regexp.Regexp { + if re, ok := r.cache.Load(expr); ok { + return re + } + + if ret, err := regexp.Compile(expr); err == nil { + r.cache.Store(expr, ret) + return ret + } else { + r.cache.Store(expr, nil) + return nil + } +} + +var replyRegexCache replyRegexCacheType + func (m *ReplyConditionTextMatch) Check(ctx *MsgContext, _ *Message, _ *CmdArgs, cleanText string) bool { var ret bool switch m.MatchType { @@ -80,8 +100,8 @@ func (m *ReplyConditionTextMatch) Check(ctx *MsgContext, _ *Message, _ *CmdArgs, case "matchSuffix": ret = strings.HasSuffix(strings.ToLower(cleanText), strings.ToLower(m.Value)) case "matchRegex": - re, err := regexp.Compile(m.Value) - if err == nil { + re := replyRegexCache.compile(m.Value) + if re != nil { lst := re.FindStringSubmatch(cleanText) gName := re.SubexpNames() for index, s := range lst { diff --git a/dice/im_session.go b/dice/im_session.go index 0a3b13a5..8f66dbec 100644 --- a/dice/im_session.go +++ b/dice/im_session.go @@ -1363,6 +1363,7 @@ func (s *IMSession) OnGroupMemberJoined(ctx *MsgContext, msg *Message) { // 借助类似操作系统信号量的思路来做一个互斥锁 var muxAutoQuit sync.Mutex var groupLeaveNum int +var platformRE = regexp.MustCompile(`^(.*)-Group:`) // LongTimeQuitInactiveGroup 另一种退群方案,其中minute代表间隔多久执行一次,num代表一次退几个群(每次退群之间有10秒的等待时间) func (s *IMSession) LongTimeQuitInactiveGroup(threshold, hint time.Time, roundIntervalMinute int, groupsPerRound int) { @@ -1386,7 +1387,6 @@ func (s *IMSession) LongTimeQuitInactiveGroup(threshold, hint time.Time, roundIn defer muxAutoQuit.Unlock() groupLeaveNum = 0 - platformRE := regexp.MustCompile(`^(.*)-Group:`) selectedGroupEndpoints := []*GroupEndpointPair{} // 创建一个存放 grp 和 ep 组合的切片 // Pinenutn: Range模板 ServiceAtNew重构代码