Skip to content

Commit

Permalink
Fix: store cache correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreamacro committed Feb 20, 2021
1 parent b3c1b4a commit a37243c
Showing 1 changed file with 8 additions and 22 deletions.
30 changes: 8 additions & 22 deletions component/profile/cachefile/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ type cache struct {
type CacheFile struct {
path string
model *cache
enc *gob.Encoder
buf *bytes.Buffer
mux sync.Mutex
}
Expand All @@ -39,15 +38,11 @@ func (c *CacheFile) SetSelected(group, selected string) {
c.mux.Lock()
defer c.mux.Unlock()

model, err := c.element()
if err != nil {
log.Warnln("[CacheFile] read cache %s failed: %s", c.path, err.Error())
return
}
model := c.element()

model.Selected[group] = selected
c.buf.Reset()
if err := c.enc.Encode(model); err != nil {
if err := gob.NewEncoder(c.buf).Encode(model); err != nil {
log.Warnln("[CacheFile] encode gob failed: %s", err.Error())
return
}
Expand All @@ -66,11 +61,7 @@ func (c *CacheFile) SelectedMap() map[string]string {
c.mux.Lock()
defer c.mux.Unlock()

model, err := c.element()
if err != nil {
log.Warnln("[CacheFile] read cache %s failed: %s", c.path, err.Error())
return nil
}
model := c.element()

mapping := map[string]string{}
for k, v := range model.Selected {
Expand All @@ -79,9 +70,9 @@ func (c *CacheFile) SelectedMap() map[string]string {
return mapping
}

func (c *CacheFile) element() (*cache, error) {
func (c *CacheFile) element() *cache {
if c.model != nil {
return c.model, nil
return c.model
}

model := &cache{
Expand All @@ -90,24 +81,19 @@ func (c *CacheFile) element() (*cache, error) {

if buf, err := ioutil.ReadFile(c.path); err == nil {
bufReader := bytes.NewBuffer(buf)
dec := gob.NewDecoder(bufReader)
if err := dec.Decode(model); err != nil {
return nil, err
}
gob.NewDecoder(bufReader).Decode(model)
}

c.model = model
return c.model, nil
return c.model
}

// Cache return singleton of CacheFile
func Cache() *CacheFile {
initOnce.Do(func() {
buf := &bytes.Buffer{}
defaultCache = &CacheFile{
path: C.Path.Cache(),
buf: buf,
enc: gob.NewEncoder(buf),
buf: &bytes.Buffer{},
}
})

Expand Down

0 comments on commit a37243c

Please sign in to comment.