From d7090689e89eba8d44ea3bdbba796b77870ccd2a Mon Sep 17 00:00:00 2001 From: astaxie Date: Thu, 24 Jul 2014 22:54:56 +0800 Subject: [PATCH] cache: change the memcache &redis driver change the memcache to the newest --- cache/memcache/memcache.go | 62 ++++++++++++++++++-------------------- cache/redis/redis.go | 2 +- cache/redis/redis_test.go | 2 +- 3 files changed, 32 insertions(+), 34 deletions(-) diff --git a/cache/memcache/memcache.go b/cache/memcache/memcache.go index 2cb478b9..7e8b6a73 100644 --- a/cache/memcache/memcache.go +++ b/cache/memcache/memcache.go @@ -12,16 +12,17 @@ package memcache import ( "encoding/json" "errors" + "strings" - "github.com/beego/memcache" + "github.com/bradfitz/gomemcache/memcache" "github.com/astaxie/beego/cache" ) // Memcache adapter. type MemcacheCache struct { - conn *memcache.Connection - conninfo string + conn *memcache.Client + conninfo []string } // create new memcache adapter. @@ -36,10 +37,8 @@ func (rc *MemcacheCache) Get(key string) interface{} { return err } } - if v, err := rc.conn.Get(key); err == nil { - if len(v) > 0 { - return string(v[0].Value) - } + if item, err := rc.conn.Get(key); err == nil { + return string(item.Value) } return nil } @@ -55,11 +54,8 @@ func (rc *MemcacheCache) Put(key string, val interface{}, timeout int64) error { if !ok { return errors.New("val must string") } - stored, err := rc.conn.Set(key, 0, uint64(timeout), []byte(v)) - if err == nil && stored == false { - return errors.New("stored fail") - } - return err + item := memcache.Item{Key: key, Value: []byte(v), Expiration: int32(timeout)} + return rc.conn.Set(&item) } // delete value in memcache. @@ -69,20 +65,29 @@ func (rc *MemcacheCache) Delete(key string) error { return err } } - _, err := rc.conn.Delete(key) + return rc.conn.Delete(key) +} + +// increase counter. +func (rc *MemcacheCache) Incr(key string) error { + if rc.conn == nil { + if err := rc.connectInit(); err != nil { + return err + } + } + _, err := rc.conn.Increment(key, 1) return err } -// [Not Support] -// increase counter. -func (rc *MemcacheCache) Incr(_ string) error { - return errors.New("not support in memcache") -} - -// [Not Support] // decrease counter. -func (rc *MemcacheCache) Decr(_ string) error { - return errors.New("not support in memcache") +func (rc *MemcacheCache) Decr(key string) error { + if rc.conn == nil { + if err := rc.connectInit(); err != nil { + return err + } + } + _, err := rc.conn.Decrement(key, 1) + return err } // check value exists in memcache. @@ -92,13 +97,10 @@ func (rc *MemcacheCache) IsExist(key string) bool { return false } } - v, err := rc.conn.Get(key) + _, err := rc.conn.Get(key) if err != nil { return false } - if len(v) == 0 { - return false - } return true } @@ -121,7 +123,7 @@ func (rc *MemcacheCache) StartAndGC(config string) error { if _, ok := cf["conn"]; !ok { return errors.New("config has no conn key") } - rc.conninfo = cf["conn"] + rc.conninfo = strings.Split(cf["conn"], ";") if rc.conn == nil { if err := rc.connectInit(); err != nil { return err @@ -132,11 +134,7 @@ func (rc *MemcacheCache) StartAndGC(config string) error { // connect to memcache and keep the connection. func (rc *MemcacheCache) connectInit() error { - c, err := memcache.Connect(rc.conninfo) - if err != nil { - return err - } - rc.conn = c + rc.conn = memcache.New(rc.conninfo...) return nil } diff --git a/cache/redis/redis.go b/cache/redis/redis.go index 1cb359e8..bd9d44c9 100644 --- a/cache/redis/redis.go +++ b/cache/redis/redis.go @@ -14,7 +14,7 @@ import ( "errors" "time" - "github.com/beego/redigo/redis" + "github.com/garyburd/redigo/redis" "github.com/astaxie/beego/cache" ) diff --git a/cache/redis/redis_test.go b/cache/redis/redis_test.go index de80d9f6..5128d249 100644 --- a/cache/redis/redis_test.go +++ b/cache/redis/redis_test.go @@ -14,7 +14,7 @@ import ( "testing" "time" - "github.com/beego/redigo/redis" + "github.com/garyburd/redigo/redis" "github.com/astaxie/beego/cache" )