cache: change the memcache &redis driver
change the memcache to the newest
Showing
3 changed files
with
30 additions
and
32 deletions
| ... | @@ -12,16 +12,17 @@ package memcache | ... | @@ -12,16 +12,17 @@ package memcache |
| 12 | import ( | 12 | import ( |
| 13 | "encoding/json" | 13 | "encoding/json" |
| 14 | "errors" | 14 | "errors" |
| 15 | "strings" | ||
| 15 | 16 | ||
| 16 | "github.com/beego/memcache" | 17 | "github.com/bradfitz/gomemcache/memcache" |
| 17 | 18 | ||
| 18 | "github.com/astaxie/beego/cache" | 19 | "github.com/astaxie/beego/cache" |
| 19 | ) | 20 | ) |
| 20 | 21 | ||
| 21 | // Memcache adapter. | 22 | // Memcache adapter. |
| 22 | type MemcacheCache struct { | 23 | type MemcacheCache struct { |
| 23 | conn *memcache.Connection | 24 | conn *memcache.Client |
| 24 | conninfo string | 25 | conninfo []string |
| 25 | } | 26 | } |
| 26 | 27 | ||
| 27 | // create new memcache adapter. | 28 | // create new memcache adapter. |
| ... | @@ -36,10 +37,8 @@ func (rc *MemcacheCache) Get(key string) interface{} { | ... | @@ -36,10 +37,8 @@ func (rc *MemcacheCache) Get(key string) interface{} { |
| 36 | return err | 37 | return err |
| 37 | } | 38 | } |
| 38 | } | 39 | } |
| 39 | if v, err := rc.conn.Get(key); err == nil { | 40 | if item, err := rc.conn.Get(key); err == nil { |
| 40 | if len(v) > 0 { | 41 | return string(item.Value) |
| 41 | return string(v[0].Value) | ||
| 42 | } | ||
| 43 | } | 42 | } |
| 44 | return nil | 43 | return nil |
| 45 | } | 44 | } |
| ... | @@ -55,11 +54,8 @@ func (rc *MemcacheCache) Put(key string, val interface{}, timeout int64) error { | ... | @@ -55,11 +54,8 @@ func (rc *MemcacheCache) Put(key string, val interface{}, timeout int64) error { |
| 55 | if !ok { | 54 | if !ok { |
| 56 | return errors.New("val must string") | 55 | return errors.New("val must string") |
| 57 | } | 56 | } |
| 58 | stored, err := rc.conn.Set(key, 0, uint64(timeout), []byte(v)) | 57 | item := memcache.Item{Key: key, Value: []byte(v), Expiration: int32(timeout)} |
| 59 | if err == nil && stored == false { | 58 | return rc.conn.Set(&item) |
| 60 | return errors.New("stored fail") | ||
| 61 | } | ||
| 62 | return err | ||
| 63 | } | 59 | } |
| 64 | 60 | ||
| 65 | // delete value in memcache. | 61 | // delete value in memcache. |
| ... | @@ -69,20 +65,29 @@ func (rc *MemcacheCache) Delete(key string) error { | ... | @@ -69,20 +65,29 @@ func (rc *MemcacheCache) Delete(key string) error { |
| 69 | return err | 65 | return err |
| 70 | } | 66 | } |
| 71 | } | 67 | } |
| 72 | _, err := rc.conn.Delete(key) | 68 | return rc.conn.Delete(key) |
| 73 | return err | ||
| 74 | } | 69 | } |
| 75 | 70 | ||
| 76 | // [Not Support] | ||
| 77 | // increase counter. | 71 | // increase counter. |
| 78 | func (rc *MemcacheCache) Incr(_ string) error { | 72 | func (rc *MemcacheCache) Incr(key string) error { |
| 79 | return errors.New("not support in memcache") | 73 | if rc.conn == nil { |
| 74 | if err := rc.connectInit(); err != nil { | ||
| 75 | return err | ||
| 76 | } | ||
| 77 | } | ||
| 78 | _, err := rc.conn.Increment(key, 1) | ||
| 79 | return err | ||
| 80 | } | 80 | } |
| 81 | 81 | ||
| 82 | // [Not Support] | ||
| 83 | // decrease counter. | 82 | // decrease counter. |
| 84 | func (rc *MemcacheCache) Decr(_ string) error { | 83 | func (rc *MemcacheCache) Decr(key string) error { |
| 85 | return errors.New("not support in memcache") | 84 | if rc.conn == nil { |
| 85 | if err := rc.connectInit(); err != nil { | ||
| 86 | return err | ||
| 87 | } | ||
| 88 | } | ||
| 89 | _, err := rc.conn.Decrement(key, 1) | ||
| 90 | return err | ||
| 86 | } | 91 | } |
| 87 | 92 | ||
| 88 | // check value exists in memcache. | 93 | // check value exists in memcache. |
| ... | @@ -92,13 +97,10 @@ func (rc *MemcacheCache) IsExist(key string) bool { | ... | @@ -92,13 +97,10 @@ func (rc *MemcacheCache) IsExist(key string) bool { |
| 92 | return false | 97 | return false |
| 93 | } | 98 | } |
| 94 | } | 99 | } |
| 95 | v, err := rc.conn.Get(key) | 100 | _, err := rc.conn.Get(key) |
| 96 | if err != nil { | 101 | if err != nil { |
| 97 | return false | 102 | return false |
| 98 | } | 103 | } |
| 99 | if len(v) == 0 { | ||
| 100 | return false | ||
| 101 | } | ||
| 102 | return true | 104 | return true |
| 103 | } | 105 | } |
| 104 | 106 | ||
| ... | @@ -121,7 +123,7 @@ func (rc *MemcacheCache) StartAndGC(config string) error { | ... | @@ -121,7 +123,7 @@ func (rc *MemcacheCache) StartAndGC(config string) error { |
| 121 | if _, ok := cf["conn"]; !ok { | 123 | if _, ok := cf["conn"]; !ok { |
| 122 | return errors.New("config has no conn key") | 124 | return errors.New("config has no conn key") |
| 123 | } | 125 | } |
| 124 | rc.conninfo = cf["conn"] | 126 | rc.conninfo = strings.Split(cf["conn"], ";") |
| 125 | if rc.conn == nil { | 127 | if rc.conn == nil { |
| 126 | if err := rc.connectInit(); err != nil { | 128 | if err := rc.connectInit(); err != nil { |
| 127 | return err | 129 | return err |
| ... | @@ -132,11 +134,7 @@ func (rc *MemcacheCache) StartAndGC(config string) error { | ... | @@ -132,11 +134,7 @@ func (rc *MemcacheCache) StartAndGC(config string) error { |
| 132 | 134 | ||
| 133 | // connect to memcache and keep the connection. | 135 | // connect to memcache and keep the connection. |
| 134 | func (rc *MemcacheCache) connectInit() error { | 136 | func (rc *MemcacheCache) connectInit() error { |
| 135 | c, err := memcache.Connect(rc.conninfo) | 137 | rc.conn = memcache.New(rc.conninfo...) |
| 136 | if err != nil { | ||
| 137 | return err | ||
| 138 | } | ||
| 139 | rc.conn = c | ||
| 140 | return nil | 138 | return nil |
| 141 | } | 139 | } |
| 142 | 140 | ... | ... |
-
Please register or sign in to post a comment