Skip to content

Commit

Permalink
fix: go-lint、移除的插件信息缓存依旧存在
Browse files Browse the repository at this point in the history
  • Loading branch information
Clov614 committed Dec 30, 2024
1 parent 5aa166a commit 9a09132
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
3 changes: 3 additions & 0 deletions rikkabot/plugins/defaultPlugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ func (af *autoAddNewFriend) recoverCache() {
af.firstRecoverFlag = true
c := af.onceDialogM.Cache.GetPluginCacheByName(autoAddnewfriendName)
bCache, err := json.Marshal(c)
if err != nil {
logging.ErrorWithErr(err, "recover auto-add-friend-cache fail")
}
err = json.Unmarshal(bCache, &af.Cache)
if err != nil {
logging.ErrorWithErr(err, "recover auto-add-friend-cache fail")
Expand Down
22 changes: 20 additions & 2 deletions rikkabot/processor/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,32 @@ type cacheExported struct {
// EnablePluginMap 获取插件状态列表 插件名-状态
func (c *Cache) EnablePluginMap() map[string]bool {
c.mu.RLock()
defer c.mu.RUnlock()
cpEnablePlugins := make(map[string]bool, len(c.EnablePlugins))
needDelPlugins := make([]string, 0, len(c.EnablePlugins))
for k, _ := range c.EnablePlugins { // 需要删除的插件信息
if !register.GetPluginPool().IsExistPlugin(k) { // 已经不在注册列表中
needDelPlugins = append(needDelPlugins, k)
}
}
for k, v := range c.EnablePlugins {
cpEnablePlugins[k] = v
if register.GetPluginPool().IsExistPlugin(k) {
cpEnablePlugins[k] = v
}
}
c.mu.RUnlock()
for _, plugin := range needDelPlugins { // 删除插件信息
c.delPlugin(plugin)
}
return cpEnablePlugins
}

// 删除插件信息
func (c *Cache) delPlugin(pluginName string) {
c.mu.Lock()
defer c.mu.Unlock()
delete(c.EnablePlugins, pluginName)
}

// 插件是否存在
func (c *Cache) isExistPlugin(pluginName string) bool {
_, ok := c.EnablePlugins[pluginName]
Expand Down
7 changes: 7 additions & 0 deletions rikkabot/processor/register/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ func (p *PluginRegister) GetPlugin(name string) IPlugin {
return p.Plugins[name]
}

func (p *PluginRegister) IsExistPlugin(name string) bool {
if p.GetPlugin(name) == nil {
return false
}
return true
}

func (p *PluginRegister) GetPluginMap() map[string]IPlugin {
p.mu.RLock()
defer p.mu.RUnlock()
Expand Down

0 comments on commit 9a09132

Please sign in to comment.