code style simplify
Showing
1 changed file
with
30 additions
and
48 deletions
| ... | @@ -7,7 +7,7 @@ | ... | @@ -7,7 +7,7 @@ |
| 7 | // @license http://github.com/astaxie/beego/blob/master/LICENSE | 7 | // @license http://github.com/astaxie/beego/blob/master/LICENSE |
| 8 | // | 8 | // |
| 9 | // @authors astaxie | 9 | // @authors astaxie |
| 10 | package cache | 10 | package memcache |
| 11 | 11 | ||
| 12 | import ( | 12 | import ( |
| 13 | "encoding/json" | 13 | "encoding/json" |
| ... | @@ -20,7 +20,7 @@ import ( | ... | @@ -20,7 +20,7 @@ import ( |
| 20 | 20 | ||
| 21 | // Memcache adapter. | 21 | // Memcache adapter. |
| 22 | type MemcacheCache struct { | 22 | type MemcacheCache struct { |
| 23 | c *memcache.Connection | 23 | conn *memcache.Connection |
| 24 | conninfo string | 24 | conninfo string |
| 25 | } | 25 | } |
| 26 | 26 | ||
| ... | @@ -31,32 +31,23 @@ func NewMemCache() *MemcacheCache { | ... | @@ -31,32 +31,23 @@ func NewMemCache() *MemcacheCache { |
| 31 | 31 | ||
| 32 | // get value from memcache. | 32 | // get value from memcache. |
| 33 | func (rc *MemcacheCache) Get(key string) interface{} { | 33 | func (rc *MemcacheCache) Get(key string) interface{} { |
| 34 | if rc.c == nil { | 34 | if rc.conn == nil { |
| 35 | var err error | 35 | if err := rc.connectInit(); err != nil { |
| 36 | rc.c, err = rc.connectInit() | ||
| 37 | if err != nil { | ||
| 38 | return err | 36 | return err |
| 39 | } | 37 | } |
| 40 | } | 38 | } |
| 41 | v, err := rc.c.Get(key) | 39 | if v, err := rc.conn.Get(key); err == nil { |
| 42 | if err != nil { | ||
| 43 | return nil | ||
| 44 | } | ||
| 45 | var contain interface{} | ||
| 46 | if len(v) > 0 { | 40 | if len(v) > 0 { |
| 47 | contain = string(v[0].Value) | 41 | return string(v[0].Value) |
| 48 | } else { | 42 | } |
| 49 | contain = nil | ||
| 50 | } | 43 | } |
| 51 | return contain | 44 | return nil |
| 52 | } | 45 | } |
| 53 | 46 | ||
| 54 | // put value to memcache. only support string. | 47 | // put value to memcache. only support string. |
| 55 | func (rc *MemcacheCache) Put(key string, val interface{}, timeout int64) error { | 48 | func (rc *MemcacheCache) Put(key string, val interface{}, timeout int64) error { |
| 56 | if rc.c == nil { | 49 | if rc.conn == nil { |
| 57 | var err error | 50 | if err := rc.connectInit(); err != nil { |
| 58 | rc.c, err = rc.connectInit() | ||
| 59 | if err != nil { | ||
| 60 | return err | 51 | return err |
| 61 | } | 52 | } |
| 62 | } | 53 | } |
| ... | @@ -64,7 +55,7 @@ func (rc *MemcacheCache) Put(key string, val interface{}, timeout int64) error { | ... | @@ -64,7 +55,7 @@ func (rc *MemcacheCache) Put(key string, val interface{}, timeout int64) error { |
| 64 | if !ok { | 55 | if !ok { |
| 65 | return errors.New("val must string") | 56 | return errors.New("val must string") |
| 66 | } | 57 | } |
| 67 | stored, err := rc.c.Set(key, 0, uint64(timeout), []byte(v)) | 58 | stored, err := rc.conn.Set(key, 0, uint64(timeout), []byte(v)) |
| 68 | if err == nil && stored == false { | 59 | if err == nil && stored == false { |
| 69 | return errors.New("stored fail") | 60 | return errors.New("stored fail") |
| 70 | } | 61 | } |
| ... | @@ -73,60 +64,52 @@ func (rc *MemcacheCache) Put(key string, val interface{}, timeout int64) error { | ... | @@ -73,60 +64,52 @@ func (rc *MemcacheCache) Put(key string, val interface{}, timeout int64) error { |
| 73 | 64 | ||
| 74 | // delete value in memcache. | 65 | // delete value in memcache. |
| 75 | func (rc *MemcacheCache) Delete(key string) error { | 66 | func (rc *MemcacheCache) Delete(key string) error { |
| 76 | if rc.c == nil { | 67 | if rc.conn == nil { |
| 77 | var err error | 68 | if err := rc.connectInit(); err != nil { |
| 78 | rc.c, err = rc.connectInit() | ||
| 79 | if err != nil { | ||
| 80 | return err | 69 | return err |
| 81 | } | 70 | } |
| 82 | } | 71 | } |
| 83 | _, err := rc.c.Delete(key) | 72 | _, err := rc.conn.Delete(key) |
| 84 | return err | 73 | return err |
| 85 | } | 74 | } |
| 86 | 75 | ||
| 87 | // [Not Support] | 76 | // [Not Support] |
| 88 | // increase counter. | 77 | // increase counter. |
| 89 | func (rc *MemcacheCache) Incr(key string) error { | 78 | func (rc *MemcacheCache) Incr(_ string) error { |
| 90 | return errors.New("not support in memcache") | 79 | return errors.New("not support in memcache") |
| 91 | } | 80 | } |
| 92 | 81 | ||
| 93 | // [Not Support] | 82 | // [Not Support] |
| 94 | // decrease counter. | 83 | // decrease counter. |
| 95 | func (rc *MemcacheCache) Decr(key string) error { | 84 | func (rc *MemcacheCache) Decr(_ string) error { |
| 96 | return errors.New("not support in memcache") | 85 | return errors.New("not support in memcache") |
| 97 | } | 86 | } |
| 98 | 87 | ||
| 99 | // check value exists in memcache. | 88 | // check value exists in memcache. |
| 100 | func (rc *MemcacheCache) IsExist(key string) bool { | 89 | func (rc *MemcacheCache) IsExist(key string) bool { |
| 101 | if rc.c == nil { | 90 | if rc.conn == nil { |
| 102 | var err error | 91 | if err := rc.connectInit(); err != nil { |
| 103 | rc.c, err = rc.connectInit() | ||
| 104 | if err != nil { | ||
| 105 | return false | 92 | return false |
| 106 | } | 93 | } |
| 107 | } | 94 | } |
| 108 | v, err := rc.c.Get(key) | 95 | v, err := rc.conn.Get(key) |
| 109 | if err != nil { | 96 | if err != nil { |
| 110 | return false | 97 | return false |
| 111 | } | 98 | } |
| 112 | if len(v) == 0 { | 99 | if len(v) == 0 { |
| 113 | return false | 100 | return false |
| 114 | } else { | ||
| 115 | return true | ||
| 116 | } | 101 | } |
| 102 | return true | ||
| 117 | } | 103 | } |
| 118 | 104 | ||
| 119 | // clear all cached in memcache. | 105 | // clear all cached in memcache. |
| 120 | func (rc *MemcacheCache) ClearAll() error { | 106 | func (rc *MemcacheCache) ClearAll() error { |
| 121 | if rc.c == nil { | 107 | if rc.conn == nil { |
| 122 | var err error | 108 | if err := rc.connectInit(); err != nil { |
| 123 | rc.c, err = rc.connectInit() | ||
| 124 | if err != nil { | ||
| 125 | return err | 109 | return err |
| 126 | } | 110 | } |
| 127 | } | 111 | } |
| 128 | err := rc.c.FlushAll() | 112 | return rc.conn.FlushAll() |
| 129 | return err | ||
| 130 | } | 113 | } |
| 131 | 114 | ||
| 132 | // start memcache adapter. | 115 | // start memcache adapter. |
| ... | @@ -139,23 +122,22 @@ func (rc *MemcacheCache) StartAndGC(config string) error { | ... | @@ -139,23 +122,22 @@ func (rc *MemcacheCache) StartAndGC(config string) error { |
| 139 | return errors.New("config has no conn key") | 122 | return errors.New("config has no conn key") |
| 140 | } | 123 | } |
| 141 | rc.conninfo = cf["conn"] | 124 | rc.conninfo = cf["conn"] |
| 142 | var err error | 125 | if rc.conn == nil { |
| 143 | if rc.c != nil { | 126 | if err := rc.connectInit(); err != nil { |
| 144 | rc.c, err = rc.connectInit() | 127 | return err |
| 145 | if err != nil { | ||
| 146 | return errors.New("dial tcp conn error") | ||
| 147 | } | 128 | } |
| 148 | } | 129 | } |
| 149 | return nil | 130 | return nil |
| 150 | } | 131 | } |
| 151 | 132 | ||
| 152 | // connect to memcache and keep the connection. | 133 | // connect to memcache and keep the connection. |
| 153 | func (rc *MemcacheCache) connectInit() (*memcache.Connection, error) { | 134 | func (rc *MemcacheCache) connectInit() error { |
| 154 | c, err := memcache.Connect(rc.conninfo) | 135 | c, err := memcache.Connect(rc.conninfo) |
| 155 | if err != nil { | 136 | if err != nil { |
| 156 | return nil, err | 137 | return err |
| 157 | } | 138 | } |
| 158 | return c, nil | 139 | rc.conn = c |
| 140 | return nil | ||
| 159 | } | 141 | } |
| 160 | 142 | ||
| 161 | func init() { | 143 | func init() { | ... | ... |
-
Please register or sign in to post a comment