From bb43d3a78c7561769e1ba2f8a7208fa3c81ef634 Mon Sep 17 00:00:00 2001 From: youngsterxyf Date: Fri, 8 Jan 2016 13:47:14 +0800 Subject: [PATCH] fix #1530 --- cache/README.md | 2 +- cache/cache.go | 7 ++++--- cache/cache_test.go | 16 +++++++++------- cache/file.go | 18 +++++++++--------- cache/memcache/memcache.go | 5 +++-- cache/memcache/memcache_test.go | 9 +++++---- cache/memory.go | 12 ++++-------- cache/redis/redis.go | 4 ++-- cache/redis/redis_test.go | 9 +++++---- 9 files changed, 42 insertions(+), 40 deletions(-) diff --git a/cache/README.md b/cache/README.md index 4152c57e..c8cb7454 100644 --- a/cache/README.md +++ b/cache/README.md @@ -26,7 +26,7 @@ Then init a Cache (example with memory adapter) Use it like this: - bm.Put("astaxie", 1, 10) + bm.Put("astaxie", 1, time.Second * 10) bm.Get("astaxie") bm.IsExist("astaxie") bm.Delete("astaxie") diff --git a/cache/cache.go b/cache/cache.go index c9904444..78643aea 100644 --- a/cache/cache.go +++ b/cache/cache.go @@ -23,7 +23,7 @@ // // Use it like this: // -// bm.Put("astaxie", 1, 10) +// bm.Put("astaxie", 1, time.Second * 10) // bm.Get("astaxie") // bm.IsExist("astaxie") // bm.Delete("astaxie") @@ -33,13 +33,14 @@ package cache import ( "fmt" + "time" ) // Cache interface contains all behaviors for cache adapter. // usage: // cache.Register("file",cache.NewFileCache) // this operation is run in init method of file.go. // c,err := cache.NewCache("file","{....}") -// c.Put("key",value,3600) +// c.Put("key",value, time.Second * 3600) // v := c.Get("key") // // c.Incr("counter") // now is 1 @@ -51,7 +52,7 @@ type Cache interface { // GetMulti is a batch version of Get. GetMulti(keys []string) []interface{} // set cached value with key and expire time. - Put(key string, val interface{}, timeout int64) error + Put(key string, val interface{}, timeout time.Duration) error // delete cached value by key. Delete(key string) error // increase cached int value by key, as a counter. diff --git a/cache/cache_test.go b/cache/cache_test.go index 481309fd..73e593e9 100644 --- a/cache/cache_test.go +++ b/cache/cache_test.go @@ -25,7 +25,8 @@ func TestCache(t *testing.T) { if err != nil { t.Error("init err") } - if err = bm.Put("astaxie", 1, 10); err != nil { + timeoutDuration := time.Second * 10 + if err = bm.Put("astaxie", 1, timeoutDuration); err != nil { t.Error("set Error", err) } if !bm.IsExist("astaxie") { @@ -42,7 +43,7 @@ func TestCache(t *testing.T) { t.Error("check err") } - if err = bm.Put("astaxie", 1, 10); err != nil { + if err = bm.Put("astaxie", 1, timeoutDuration); err != nil { t.Error("set Error", err) } @@ -67,7 +68,7 @@ func TestCache(t *testing.T) { } //test GetMulti - if err = bm.Put("astaxie", "author", 10); err != nil { + if err = bm.Put("astaxie", "author", timeoutDuration); err != nil { t.Error("set Error", err) } if !bm.IsExist("astaxie") { @@ -77,7 +78,7 @@ func TestCache(t *testing.T) { t.Error("get err") } - if err = bm.Put("astaxie1", "author1", 10); err != nil { + if err = bm.Put("astaxie1", "author1", timeoutDuration); err != nil { t.Error("set Error", err) } if !bm.IsExist("astaxie1") { @@ -101,7 +102,8 @@ func TestFileCache(t *testing.T) { if err != nil { t.Error("init err") } - if err = bm.Put("astaxie", 1, 10); err != nil { + timeoutDuration := time.Second * 10 + if err = bm.Put("astaxie", 1, timeoutDuration); err != nil { t.Error("set Error", err) } if !bm.IsExist("astaxie") { @@ -133,7 +135,7 @@ func TestFileCache(t *testing.T) { } //test string - if err = bm.Put("astaxie", "author", 10); err != nil { + if err = bm.Put("astaxie", "author", timeoutDuration); err != nil { t.Error("set Error", err) } if !bm.IsExist("astaxie") { @@ -144,7 +146,7 @@ func TestFileCache(t *testing.T) { } //test GetMulti - if err = bm.Put("astaxie1", "author1", 10); err != nil { + if err = bm.Put("astaxie1", "author1", timeoutDuration); err != nil { t.Error("set Error", err) } if !bm.IsExist("astaxie1") { diff --git a/cache/file.go b/cache/file.go index 2d77b567..80f93bce 100644 --- a/cache/file.go +++ b/cache/file.go @@ -33,8 +33,8 @@ import ( // it contains data and expire time. type FileCacheItem struct { Data interface{} - Lastaccess int64 - Expired int64 + Lastaccess time.Time + Expired time.Time } // FileCache Config @@ -42,7 +42,7 @@ var ( FileCachePath = "cache" // cache directory FileCacheFileSuffix = ".bin" // cache file suffix FileCacheDirectoryLevel = 2 // cache file deep level if auto generated cache files. - FileCacheEmbedExpiry int64 // cache expire time, default is no expire forever. + FileCacheEmbedExpiry time.Duration = 0 // cache expire time, default is no expire forever. ) // FileCache is cache adapter for file storage. @@ -76,7 +76,7 @@ func (fc *FileCache) StartAndGC(config string) error { cfg["DirectoryLevel"] = strconv.Itoa(FileCacheDirectoryLevel) } if _, ok := cfg["EmbedExpiry"]; !ok { - cfg["EmbedExpiry"] = strconv.FormatInt(FileCacheEmbedExpiry, 10) + cfg["EmbedExpiry"] = strconv.FormatInt(int64(FileCacheEmbedExpiry.Seconds()), 10) } fc.CachePath = cfg["CachePath"] fc.FileSuffix = cfg["FileSuffix"] @@ -123,7 +123,7 @@ func (fc *FileCache) Get(key string) interface{} { } var to FileCacheItem GobDecode(fileData, &to) - if to.Expired < time.Now().Unix() { + if to.Expired.Before(time.Now()) { return "" } return to.Data @@ -142,16 +142,16 @@ 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. -func (fc *FileCache) Put(key string, val interface{}, timeout int64) error { +func (fc *FileCache) Put(key string, val interface{}, timeout time.Duration) error { gob.Register(val) item := FileCacheItem{Data: val} if timeout == FileCacheEmbedExpiry { - item.Expired = time.Now().Unix() + (86400 * 365 * 10) // ten years + item.Expired = time.Now().Add(time.Second * (86400 * 365 * 10)) // ten years } else { - item.Expired = time.Now().Unix() + timeout + item.Expired = time.Now().Add(timeout) } - item.Lastaccess = time.Now().Unix() + item.Lastaccess = time.Now() data, err := GobEncode(item) if err != nil { return err diff --git a/cache/memcache/memcache.go b/cache/memcache/memcache.go index 272124da..ea77be38 100644 --- a/cache/memcache/memcache.go +++ b/cache/memcache/memcache.go @@ -37,6 +37,7 @@ import ( "github.com/bradfitz/gomemcache/memcache" "github.com/astaxie/beego/cache" + "time" ) // Cache Memcache adapter. @@ -89,7 +90,7 @@ func (rc *Cache) GetMulti(keys []string) []interface{} { } // Put put value to memcache. only support string. -func (rc *Cache) Put(key string, val interface{}, timeout int64) error { +func (rc *Cache) Put(key string, val interface{}, timeout time.Duration) error { if rc.conn == nil { if err := rc.connectInit(); err != nil { return err @@ -99,7 +100,7 @@ func (rc *Cache) Put(key string, val interface{}, timeout int64) error { if !ok { return errors.New("val must string") } - item := memcache.Item{Key: key, Value: []byte(v), Expiration: int32(timeout)} + item := memcache.Item{Key: key, Value: []byte(v), Expiration: int32(timeout.Seconds())} return rc.conn.Set(&item) } diff --git a/cache/memcache/memcache_test.go b/cache/memcache/memcache_test.go index 27b085e6..93484f6b 100644 --- a/cache/memcache/memcache_test.go +++ b/cache/memcache/memcache_test.go @@ -28,7 +28,8 @@ func TestMemcacheCache(t *testing.T) { if err != nil { t.Error("init err") } - if err = bm.Put("astaxie", "1", 10); err != nil { + timeoutDuration := time.Second * 10 + if err = bm.Put("astaxie", "1", timeoutDuration); err != nil { t.Error("set Error", err) } if !bm.IsExist("astaxie") { @@ -40,7 +41,7 @@ func TestMemcacheCache(t *testing.T) { if bm.IsExist("astaxie") { t.Error("check err") } - if err = bm.Put("astaxie", "1", 10); err != nil { + if err = bm.Put("astaxie", "1", timeoutDuration); err != nil { t.Error("set Error", err) } @@ -69,7 +70,7 @@ func TestMemcacheCache(t *testing.T) { } //test string - if err = bm.Put("astaxie", "author", 10); err != nil { + if err = bm.Put("astaxie", "author", timeoutDuration); err != nil { t.Error("set Error", err) } if !bm.IsExist("astaxie") { @@ -81,7 +82,7 @@ func TestMemcacheCache(t *testing.T) { } //test GetMulti - if err = bm.Put("astaxie1", "author1", 10); err != nil { + if err = bm.Put("astaxie1", "author1", timeoutDuration); err != nil { t.Error("set Error", err) } if !bm.IsExist("astaxie1") { diff --git a/cache/memory.go b/cache/memory.go index f8cccb14..5ba11b3c 100644 --- a/cache/memory.go +++ b/cache/memory.go @@ -17,7 +17,6 @@ package cache import ( "encoding/json" "errors" - "fmt" "sync" "time" ) @@ -31,7 +30,7 @@ var ( type MemoryItem struct { val interface{} createdTime time.Time - lifespan int64 + lifespan time.Duration } func (mi *MemoryItem) isExpire() bool { @@ -39,7 +38,7 @@ func (mi *MemoryItem) isExpire() bool { if mi.lifespan == 0 { return false } - return time.Now().Unix()-mi.createdTime.Unix() > mi.lifespan + return time.Now().Sub(mi.createdTime) > mi.lifespan } // MemoryCache is Memory cache adapter. @@ -83,7 +82,7 @@ func (bc *MemoryCache) GetMulti(names []string) []interface{} { // Put cache to memory. // if lifespan is 0, it will be forever till restart. -func (bc *MemoryCache) Put(name string, value interface{}, lifespan int64) error { +func (bc *MemoryCache) Put(name string, value interface{}, lifespan time.Duration) error { bc.Lock() defer bc.Unlock() bc.items[name] = &MemoryItem{ @@ -201,10 +200,7 @@ func (bc *MemoryCache) StartAndGC(config string) error { cf = make(map[string]int) cf["interval"] = DefaultEvery } - dur, err := time.ParseDuration(fmt.Sprintf("%ds", cf["interval"])) - if err != nil { - return err - } + dur := time.Second * time.Duration(cf["interval"]) bc.Every = cf["interval"] bc.dur = dur go bc.vaccuum() diff --git a/cache/redis/redis.go b/cache/redis/redis.go index 60c1702a..316cb423 100644 --- a/cache/redis/redis.go +++ b/cache/redis/redis.go @@ -109,9 +109,9 @@ ERROR: } // Put put cache to redis. -func (rc *Cache) Put(key string, val interface{}, timeout int64) error { +func (rc *Cache) Put(key string, val interface{}, timeout time.Duration) error { var err error - if _, err = rc.do("SETEX", key, timeout, val); err != nil { + if _, err = rc.do("SETEX", key, int64(timeout.Seconds()), val); err != nil { return err } diff --git a/cache/redis/redis_test.go b/cache/redis/redis_test.go index 1f74fd27..22059508 100644 --- a/cache/redis/redis_test.go +++ b/cache/redis/redis_test.go @@ -28,7 +28,8 @@ func TestRedisCache(t *testing.T) { if err != nil { t.Error("init err") } - if err = bm.Put("astaxie", 1, 10); err != nil { + timeoutDuration := time.Second * 10 + if err = bm.Put("astaxie", 1, timeoutDuration); err != nil { t.Error("set Error", err) } if !bm.IsExist("astaxie") { @@ -40,7 +41,7 @@ func TestRedisCache(t *testing.T) { if bm.IsExist("astaxie") { t.Error("check err") } - if err = bm.Put("astaxie", 1, 10); err != nil { + if err = bm.Put("astaxie", 1, timeoutDuration); err != nil { t.Error("set Error", err) } @@ -69,7 +70,7 @@ func TestRedisCache(t *testing.T) { } //test string - if err = bm.Put("astaxie", "author", 10); err != nil { + if err = bm.Put("astaxie", "author", timeoutDuration); err != nil { t.Error("set Error", err) } if !bm.IsExist("astaxie") { @@ -81,7 +82,7 @@ func TestRedisCache(t *testing.T) { } //test GetMulti - if err = bm.Put("astaxie1", "author1", 10); err != nil { + if err = bm.Put("astaxie1", "author1", timeoutDuration); err != nil { t.Error("set Error", err) } if !bm.IsExist("astaxie1") {