Skip to content

Commit

Permalink
feat: 缓存已注册的插件记录Rd
Browse files Browse the repository at this point in the history
  • Loading branch information
rehiy committed Mar 15, 2024
1 parent a7478d3 commit 7c6ee75
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 5 deletions.
2 changes: 1 addition & 1 deletion dbase/cronjob/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func Update(data *UpdateParam) error {

}

// 合并联系人
// 合并计划

type ReplaceParam = CreateParam

Expand Down
84 changes: 84 additions & 0 deletions wclient/plugin/cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package plugin

import (
"fmt"
"os"
"path"
"strconv"
"strings"
"sync"
)

type Cache struct {
mu sync.RWMutex
data map[string]uint
file string
}

func NewCache(file string) *Cache {

c := &Cache{file: file}
return c.init()

}

func (c *Cache) Put(k string, v uint) {

c.data[k] = v
c.save()

}

func (c *Cache) Get(k string) uint {

return c.data[k]

}

func (c *Cache) Del(k string) {

delete(c.data, k)
c.save()

}

func (c *Cache) init() *Cache {

c.mu.Lock()
defer c.mu.Unlock()

data := map[string]uint{}

temp, _ := os.ReadFile(c.file)
list := strings.Split(string(temp), "\n")

for _, line := range list {
if strings.Contains(line, "=") {
x := strings.Split(strings.TrimSpace(line), "=")
v, _ := strconv.ParseInt(x[0], 10, 32)
data[x[1]] = uint(v)
}
}

c.data = data
return c

}

func (c *Cache) save() *Cache {

c.mu.Lock()
defer c.mu.Unlock()

list := []string{}
for k, v := range c.data {
list = append(list, fmt.Sprintf("%d=%s", v, k))
}

data := strings.Join(list, "\n")
os.MkdirAll(path.Dir(c.file), 0755)
os.WriteFile(c.file, []byte(data), 0644)

return c

}
9 changes: 7 additions & 2 deletions wclient/plugin/cronjob.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type CronjobPlugin struct {
func CronjobPluginSetup() ([]*CronjobPlugin, error) {

configs := []*CronjobPlugin{}
checker := NewCache("./plugin/cronjob.txt")

err := filepath.Walk("./plugin/cronjob", func(rp string, info os.FileInfo, err error) error {
if err != nil || info.IsDir() {
Expand All @@ -37,8 +38,12 @@ func CronjobPluginSetup() ([]*CronjobPlugin, error) {
}
// 更新插件信息
errstr := ""
if err := cronjob.Replace(config); err != nil {
errstr = err.Error()
if checker.Get(rp) == 0 {
if rd, err := cronjob.Create(config); err == nil {
checker.Put(rp, rd)
} else {
errstr = err.Error()
}
}
configs = append(configs, &CronjobPlugin{
config, errstr, info.Name(),
Expand Down
9 changes: 7 additions & 2 deletions wclient/plugin/keyword.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type KeywordPlugin struct {
func KeywordPluginSetup() ([]*KeywordPlugin, error) {

configs := []*KeywordPlugin{}
checker := NewCache("./plugin/keyword.txt")

err := filepath.Walk("./plugin/keyword", func(rp string, info os.FileInfo, err error) error {
if err != nil || info.IsDir() {
Expand All @@ -37,8 +38,12 @@ func KeywordPluginSetup() ([]*KeywordPlugin, error) {
}
// 更新插件信息
errstr := ""
if err := keyword.Replace(config); err != nil {
errstr = err.Error()
if checker.Get(rp) == 0 {
if rd, err := keyword.Create(config); err == nil {
checker.Put(rp, rd)
} else {
errstr = err.Error()
}
}
configs = append(configs, &KeywordPlugin{
config, errstr, info.Name(),
Expand Down

0 comments on commit 7c6ee75

Please sign in to comment.