Skip to content

Commit

Permalink
fix bug on cache/file
Browse files Browse the repository at this point in the history
  • Loading branch information
Wusuluren committed Apr 5, 2019
1 parent c0ecf32 commit 1900246
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions cache/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,14 @@ func NewFileCache() Cache {
}

// StartAndGC will start and begin gc for file cache.
// the config need to be like {CachePath:"/cache","FileSuffix":".bin","DirectoryLevel":2,"EmbedExpiry":0}
// the config need to be like {CachePath:"/cache","FileSuffix":".bin","DirectoryLevel":"2","EmbedExpiry":"0"}
func (fc *FileCache) StartAndGC(config string) error {

cfg := make(map[string]string)
json.Unmarshal([]byte(config), &cfg)
err := json.Unmarshal([]byte(config), &cfg)
if err != nil {
return err
}
if _, ok := cfg["CachePath"]; !ok {
cfg["CachePath"] = FileCachePath
}
Expand Down Expand Up @@ -142,12 +145,12 @@ func (fc *FileCache) GetMulti(keys []string) []interface{} {

// Put value into file cache.
// timeout means how long to keep this file, unit of ms.
// if timeout equals FileCacheEmbedExpiry(default is 0), cache this item forever.
// if timeout equals fc.EmbedExpiry(default is 0), cache this item forever.
func (fc *FileCache) Put(key string, val interface{}, timeout time.Duration) error {
gob.Register(val)

item := FileCacheItem{Data: val}
if timeout == FileCacheEmbedExpiry {
if timeout == time.Duration(fc.EmbedExpiry) {
item.Expired = time.Now().Add((86400 * 365 * 10) * time.Second) // ten years
} else {
item.Expired = time.Now().Add(timeout)
Expand Down Expand Up @@ -179,7 +182,7 @@ func (fc *FileCache) Incr(key string) error {
} else {
incr = data.(int) + 1
}
fc.Put(key, incr, FileCacheEmbedExpiry)
fc.Put(key, incr, time.Duration(fc.EmbedExpiry))
return nil
}

Expand All @@ -192,7 +195,7 @@ func (fc *FileCache) Decr(key string) error {
} else {
decr = data.(int) - 1
}
fc.Put(key, decr, FileCacheEmbedExpiry)
fc.Put(key, decr, time.Duration(fc.EmbedExpiry))
return nil
}

Expand Down

0 comments on commit 1900246

Please sign in to comment.