Skip to content
This repository has been archived by the owner on Feb 28, 2023. It is now read-only.

Commit

Permalink
fix: Cache add mutex lock
Browse files Browse the repository at this point in the history
Signed-off-by: qwqcode <[email protected]>
  • Loading branch information
qwqcode committed Apr 23, 2022
1 parent d1986c4 commit 0ab5d1f
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions model/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@ import (
"encoding/json"
"fmt"
"strings"
"sync"

"github.com/ArtalkJS/ArtalkGo/lib"
"github.com/eko/gocache/v2/store"
"github.com/sirupsen/logrus"
)

var (
MutexCache = sync.Mutex{}
)

type cacher struct{ cacheKey string }

func (c *cacher) StoreCache(srcStruct interface{}) error {
Expand Down Expand Up @@ -38,6 +43,9 @@ func FindCache(name string, destStruct interface{}) (cacher, error) {
}

func StoreCache(name string, srcStruct interface{}) error {
MutexCache.Lock()
defer MutexCache.Unlock()

str, err := json.Marshal(srcStruct)
if err != nil {
return err
Expand All @@ -48,10 +56,15 @@ func StoreCache(name string, srcStruct interface{}) error {
return err
}

logrus.Debug("[写入缓存] " + name)

return nil
}

func ClearCache(name string) error {
MutexCache.Lock()
defer MutexCache.Unlock()

return lib.CACHE.Delete(lib.Ctx, name)
}

Expand Down

0 comments on commit 0ab5d1f

Please sign in to comment.