Fix: store cache correctly
This commit is contained in:
parent
498d92d5b2
commit
b2e4a0b63d
1 changed files with 8 additions and 22 deletions
|
@ -26,7 +26,6 @@ type cache struct {
|
||||||
type CacheFile struct {
|
type CacheFile struct {
|
||||||
path string
|
path string
|
||||||
model *cache
|
model *cache
|
||||||
enc *gob.Encoder
|
|
||||||
buf *bytes.Buffer
|
buf *bytes.Buffer
|
||||||
mux sync.Mutex
|
mux sync.Mutex
|
||||||
}
|
}
|
||||||
|
@ -39,15 +38,11 @@ func (c *CacheFile) SetSelected(group, selected string) {
|
||||||
c.mux.Lock()
|
c.mux.Lock()
|
||||||
defer c.mux.Unlock()
|
defer c.mux.Unlock()
|
||||||
|
|
||||||
model, err := c.element()
|
model := c.element()
|
||||||
if err != nil {
|
|
||||||
log.Warnln("[CacheFile] read cache %s failed: %s", c.path, err.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
model.Selected[group] = selected
|
model.Selected[group] = selected
|
||||||
c.buf.Reset()
|
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())
|
log.Warnln("[CacheFile] encode gob failed: %s", err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -66,11 +61,7 @@ func (c *CacheFile) SelectedMap() map[string]string {
|
||||||
c.mux.Lock()
|
c.mux.Lock()
|
||||||
defer c.mux.Unlock()
|
defer c.mux.Unlock()
|
||||||
|
|
||||||
model, err := c.element()
|
model := c.element()
|
||||||
if err != nil {
|
|
||||||
log.Warnln("[CacheFile] read cache %s failed: %s", c.path, err.Error())
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
mapping := map[string]string{}
|
mapping := map[string]string{}
|
||||||
for k, v := range model.Selected {
|
for k, v := range model.Selected {
|
||||||
|
@ -79,9 +70,9 @@ func (c *CacheFile) SelectedMap() map[string]string {
|
||||||
return mapping
|
return mapping
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *CacheFile) element() (*cache, error) {
|
func (c *CacheFile) element() *cache {
|
||||||
if c.model != nil {
|
if c.model != nil {
|
||||||
return c.model, nil
|
return c.model
|
||||||
}
|
}
|
||||||
|
|
||||||
model := &cache{
|
model := &cache{
|
||||||
|
@ -90,24 +81,19 @@ func (c *CacheFile) element() (*cache, error) {
|
||||||
|
|
||||||
if buf, err := ioutil.ReadFile(c.path); err == nil {
|
if buf, err := ioutil.ReadFile(c.path); err == nil {
|
||||||
bufReader := bytes.NewBuffer(buf)
|
bufReader := bytes.NewBuffer(buf)
|
||||||
dec := gob.NewDecoder(bufReader)
|
gob.NewDecoder(bufReader).Decode(model)
|
||||||
if err := dec.Decode(model); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
c.model = model
|
c.model = model
|
||||||
return c.model, nil
|
return c.model
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cache return singleton of CacheFile
|
// Cache return singleton of CacheFile
|
||||||
func Cache() *CacheFile {
|
func Cache() *CacheFile {
|
||||||
initOnce.Do(func() {
|
initOnce.Do(func() {
|
||||||
buf := &bytes.Buffer{}
|
|
||||||
defaultCache = &CacheFile{
|
defaultCache = &CacheFile{
|
||||||
path: C.Path.Cache(),
|
path: C.Path.Cache(),
|
||||||
buf: buf,
|
buf: &bytes.Buffer{},
|
||||||
enc: gob.NewEncoder(buf),
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue