1
0
mirror of https://github.com/astaxie/beego.git synced 2025-06-11 08:30:40 +00:00
This commit is contained in:
youngsterxyf
2016-01-08 13:47:14 +08:00
parent db2918b0aa
commit bb43d3a78c
9 changed files with 42 additions and 40 deletions

View File

@ -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)
}

View File

@ -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") {