mirror of
https://github.com/astaxie/beego.git
synced 2024-11-22 17:40:55 +00:00
Merge pull request #3599 from Wusuluren/cache_file
fix bug on cache/file
This commit is contained in:
commit
0c576dac82
2
cache/cache_test.go
vendored
2
cache/cache_test.go
vendored
@ -98,7 +98,7 @@ func TestCache(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestFileCache(t *testing.T) {
|
func TestFileCache(t *testing.T) {
|
||||||
bm, err := NewCache("file", `{"CachePath":"cache","FileSuffix":".bin","DirectoryLevel":2,"EmbedExpiry":0}`)
|
bm, err := NewCache("file", `{"CachePath":"cache","FileSuffix":".bin","DirectoryLevel":"2","EmbedExpiry":"0"}`)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error("init err")
|
t.Error("init err")
|
||||||
}
|
}
|
||||||
|
15
cache/file.go
vendored
15
cache/file.go
vendored
@ -62,11 +62,14 @@ func NewFileCache() Cache {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// StartAndGC will start and begin gc for file 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 {
|
func (fc *FileCache) StartAndGC(config string) error {
|
||||||
|
|
||||||
cfg := make(map[string]string)
|
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 {
|
if _, ok := cfg["CachePath"]; !ok {
|
||||||
cfg["CachePath"] = FileCachePath
|
cfg["CachePath"] = FileCachePath
|
||||||
}
|
}
|
||||||
@ -142,12 +145,12 @@ func (fc *FileCache) GetMulti(keys []string) []interface{} {
|
|||||||
|
|
||||||
// Put value into file cache.
|
// Put value into file cache.
|
||||||
// timeout means how long to keep this file, unit of ms.
|
// 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 {
|
func (fc *FileCache) Put(key string, val interface{}, timeout time.Duration) error {
|
||||||
gob.Register(val)
|
gob.Register(val)
|
||||||
|
|
||||||
item := FileCacheItem{Data: 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
|
item.Expired = time.Now().Add((86400 * 365 * 10) * time.Second) // ten years
|
||||||
} else {
|
} else {
|
||||||
item.Expired = time.Now().Add(timeout)
|
item.Expired = time.Now().Add(timeout)
|
||||||
@ -179,7 +182,7 @@ func (fc *FileCache) Incr(key string) error {
|
|||||||
} else {
|
} else {
|
||||||
incr = data.(int) + 1
|
incr = data.(int) + 1
|
||||||
}
|
}
|
||||||
fc.Put(key, incr, FileCacheEmbedExpiry)
|
fc.Put(key, incr, time.Duration(fc.EmbedExpiry))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -192,7 +195,7 @@ func (fc *FileCache) Decr(key string) error {
|
|||||||
} else {
|
} else {
|
||||||
decr = data.(int) - 1
|
decr = data.(int) - 1
|
||||||
}
|
}
|
||||||
fc.Put(key, decr, FileCacheEmbedExpiry)
|
fc.Put(key, decr, time.Duration(fc.EmbedExpiry))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user