0b42e557 by Pengfei Xue

align memcache operations with redis

1 parent a369b15e
...@@ -21,7 +21,11 @@ func NewMemCache() *MemcacheCache { ...@@ -21,7 +21,11 @@ func NewMemCache() *MemcacheCache {
21 // get value from memcache. 21 // get value from memcache.
22 func (rc *MemcacheCache) Get(key string) interface{} { 22 func (rc *MemcacheCache) Get(key string) interface{} {
23 if rc.c == nil { 23 if rc.c == nil {
24 rc.c = rc.connectInit() 24 var err error
25 rc.c, err = rc.connectInit()
26 if err != nil {
27 return err
28 }
25 } 29 }
26 v, err := rc.c.Get(key) 30 v, err := rc.c.Get(key)
27 if err != nil { 31 if err != nil {
...@@ -39,7 +43,11 @@ func (rc *MemcacheCache) Get(key string) interface{} { ...@@ -39,7 +43,11 @@ func (rc *MemcacheCache) Get(key string) interface{} {
39 // put value to memcache. only support string. 43 // put value to memcache. only support string.
40 func (rc *MemcacheCache) Put(key string, val interface{}, timeout int64) error { 44 func (rc *MemcacheCache) Put(key string, val interface{}, timeout int64) error {
41 if rc.c == nil { 45 if rc.c == nil {
42 rc.c = rc.connectInit() 46 var err error
47 rc.c, err = rc.connectInit()
48 if err != nil {
49 return err
50 }
43 } 51 }
44 v, ok := val.(string) 52 v, ok := val.(string)
45 if !ok { 53 if !ok {
...@@ -55,7 +63,11 @@ func (rc *MemcacheCache) Put(key string, val interface{}, timeout int64) error { ...@@ -55,7 +63,11 @@ func (rc *MemcacheCache) Put(key string, val interface{}, timeout int64) error {
55 // delete value in memcache. 63 // delete value in memcache.
56 func (rc *MemcacheCache) Delete(key string) error { 64 func (rc *MemcacheCache) Delete(key string) error {
57 if rc.c == nil { 65 if rc.c == nil {
58 rc.c = rc.connectInit() 66 var err error
67 rc.c, err = rc.connectInit()
68 if err != nil {
69 return err
70 }
59 } 71 }
60 _, err := rc.c.Delete(key) 72 _, err := rc.c.Delete(key)
61 return err 73 return err
...@@ -76,7 +88,11 @@ func (rc *MemcacheCache) Decr(key string) error { ...@@ -76,7 +88,11 @@ func (rc *MemcacheCache) Decr(key string) error {
76 // check value exists in memcache. 88 // check value exists in memcache.
77 func (rc *MemcacheCache) IsExist(key string) bool { 89 func (rc *MemcacheCache) IsExist(key string) bool {
78 if rc.c == nil { 90 if rc.c == nil {
79 rc.c = rc.connectInit() 91 var err error
92 rc.c, err = rc.connectInit()
93 if err != nil {
94 return false
95 }
80 } 96 }
81 v, err := rc.c.Get(key) 97 v, err := rc.c.Get(key)
82 if err != nil { 98 if err != nil {
...@@ -93,7 +109,11 @@ func (rc *MemcacheCache) IsExist(key string) bool { ...@@ -93,7 +109,11 @@ func (rc *MemcacheCache) IsExist(key string) bool {
93 // clear all cached in memcache. 109 // clear all cached in memcache.
94 func (rc *MemcacheCache) ClearAll() error { 110 func (rc *MemcacheCache) ClearAll() error {
95 if rc.c == nil { 111 if rc.c == nil {
96 rc.c = rc.connectInit() 112 var err error
113 rc.c, err = rc.connectInit()
114 if err != nil {
115 return err
116 }
97 } 117 }
98 err := rc.c.FlushAll() 118 err := rc.c.FlushAll()
99 return err 119 return err
...@@ -109,20 +129,21 @@ func (rc *MemcacheCache) StartAndGC(config string) error { ...@@ -109,20 +129,21 @@ func (rc *MemcacheCache) StartAndGC(config string) error {
109 return errors.New("config has no conn key") 129 return errors.New("config has no conn key")
110 } 130 }
111 rc.conninfo = cf["conn"] 131 rc.conninfo = cf["conn"]
112 rc.c = rc.connectInit() 132 var err error
113 if rc.c == nil { 133 rc.c, err = rc.connectInit()
134 if err != nil {
114 return errors.New("dial tcp conn error") 135 return errors.New("dial tcp conn error")
115 } 136 }
116 return nil 137 return nil
117 } 138 }
118 139
119 // connect to memcache and keep the connection. 140 // connect to memcache and keep the connection.
120 func (rc *MemcacheCache) connectInit() *memcache.Connection { 141 func (rc *MemcacheCache) connectInit() (*memcache.Connection, error) {
121 c, err := memcache.Connect(rc.conninfo) 142 c, err := memcache.Connect(rc.conninfo)
122 if err != nil { 143 if err != nil {
123 return nil 144 return nil, err
124 } 145 }
125 return c 146 return c, nil
126 } 147 }
127 148
128 func init() { 149 func init() {
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!