77c40e6f by fuxiaohei

code style simplify

1 parent ea2ed90a
...@@ -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 redis
11 11
12 import ( 12 import (
13 "encoding/json" 13 "encoding/json"
...@@ -46,23 +46,21 @@ func (rc *RedisCache) do(commandName string, args ...interface{}) (reply interfa ...@@ -46,23 +46,21 @@ func (rc *RedisCache) do(commandName string, args ...interface{}) (reply interfa
46 46
47 // Get cache from redis. 47 // Get cache from redis.
48 func (rc *RedisCache) Get(key string) interface{} { 48 func (rc *RedisCache) Get(key string) interface{} {
49 v, err := rc.do("GET", key) 49 if v, err := rc.do("GET", key); err == nil {
50 if err != nil { 50 return v
51 return nil
52 } 51 }
53 52 return nil
54 return v
55 } 53 }
56 54
57 // put cache to redis. 55 // put cache to redis.
58 func (rc *RedisCache) Put(key string, val interface{}, timeout int64) error { 56 func (rc *RedisCache) Put(key string, val interface{}, timeout int64) error {
59 _, err := rc.do("SET", key, val) 57 var err error
60 if err != nil { 58 if _, err = rc.do("SET", key, val); err != nil {
61 return nil 59 return err
62 } 60 }
63 _, err = rc.do("HSET", rc.key, key, true) 61
64 if err != nil { 62 if _, err = rc.do("HSET", rc.key, key, true); err != nil {
65 return nil 63 return err
66 } 64 }
67 _, err = rc.do("EXPIRE", key, timeout) 65 _, err = rc.do("EXPIRE", key, timeout)
68 return err 66 return err
...@@ -70,9 +68,9 @@ func (rc *RedisCache) Put(key string, val interface{}, timeout int64) error { ...@@ -70,9 +68,9 @@ func (rc *RedisCache) Put(key string, val interface{}, timeout int64) error {
70 68
71 // delete cache in redis. 69 // delete cache in redis.
72 func (rc *RedisCache) Delete(key string) error { 70 func (rc *RedisCache) Delete(key string) error {
73 _, err := rc.do("DEL", key) 71 var err error
74 if err != nil { 72 if _, err = rc.do("DEL", key); err != nil {
75 return nil 73 return err
76 } 74 }
77 _, err = rc.do("HDEL", rc.key, key) 75 _, err = rc.do("HDEL", rc.key, key)
78 return err 76 return err
...@@ -85,8 +83,7 @@ func (rc *RedisCache) IsExist(key string) bool { ...@@ -85,8 +83,7 @@ func (rc *RedisCache) IsExist(key string) bool {
85 return false 83 return false
86 } 84 }
87 if v == false { 85 if v == false {
88 _, err := rc.do("HDEL", rc.key, key) 86 if _, err = rc.do("HDEL", rc.key, key); err != nil {
89 if err != nil {
90 return false 87 return false
91 } 88 }
92 } 89 }
...@@ -108,10 +105,12 @@ func (rc *RedisCache) Decr(key string) error { ...@@ -108,10 +105,12 @@ func (rc *RedisCache) Decr(key string) error {
108 // clean all cache in redis. delete this redis collection. 105 // clean all cache in redis. delete this redis collection.
109 func (rc *RedisCache) ClearAll() error { 106 func (rc *RedisCache) ClearAll() error {
110 cachedKeys, err := redis.Strings(rc.do("HKEYS", rc.key)) 107 cachedKeys, err := redis.Strings(rc.do("HKEYS", rc.key))
108 if err != nil {
109 return err
110 }
111 for _, str := range cachedKeys { 111 for _, str := range cachedKeys {
112 _, err := rc.do("DEL", str) 112 if _, err = rc.do("DEL", str); err != nil {
113 if err != nil { 113 return err
114 return nil
115 } 114 }
116 } 115 }
117 _, err = rc.do("DEL", rc.key) 116 _, err = rc.do("DEL", rc.key)
...@@ -140,26 +139,21 @@ func (rc *RedisCache) StartAndGC(config string) error { ...@@ -140,26 +139,21 @@ func (rc *RedisCache) StartAndGC(config string) error {
140 139
141 c := rc.p.Get() 140 c := rc.p.Get()
142 defer c.Close() 141 defer c.Close()
143 if err := c.Err(); err != nil {
144 return err
145 }
146 142
147 return nil 143 return c.Err()
148 } 144 }
149 145
150 // connect to redis. 146 // connect to redis.
151 func (rc *RedisCache) connectInit() { 147 func (rc *RedisCache) connectInit() {
148 dialFunc := func() (c redis.Conn, err error) {
149 c, err = redis.Dial("tcp", rc.conninfo)
150 return
151 }
152 // initialize a new pool 152 // initialize a new pool
153 rc.p = &redis.Pool{ 153 rc.p = &redis.Pool{
154 MaxIdle: 3, 154 MaxIdle: 3,
155 IdleTimeout: 180 * time.Second, 155 IdleTimeout: 180 * time.Second,
156 Dial: func() (redis.Conn, error) { 156 Dial: dialFunc,
157 c, err := redis.Dial("tcp", rc.conninfo)
158 if err != nil {
159 return nil, err
160 }
161 return c, nil
162 },
163 } 157 }
164 } 158 }
165 159
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
8 8
9 // @authors astaxie 9 // @authors astaxie
10 10
11 package cache 11 package redis
12 12
13 import ( 13 import (
14 "testing" 14 "testing"
...@@ -20,7 +20,7 @@ import ( ...@@ -20,7 +20,7 @@ import (
20 ) 20 )
21 21
22 func TestRedisCache(t *testing.T) { 22 func TestRedisCache(t *testing.T) {
23 bm, err := cache.NewCache("redis", `{"conn": "127.0.0.1:6379"}`) 23 bm, err := cache.NewCache("redis", `{"conn": "127.0.0.1:6379"}`)
24 if err != nil { 24 if err != nil {
25 t.Error("init err") 25 t.Error("init err")
26 } 26 }
...@@ -48,7 +48,7 @@ func TestRedisCache(t *testing.T) { ...@@ -48,7 +48,7 @@ func TestRedisCache(t *testing.T) {
48 t.Error("Incr Error", err) 48 t.Error("Incr Error", err)
49 } 49 }
50 50
51 if v, _ := redis.Int(bm.Get("astaxie"), err); v != 2 { 51 if v, _ := redis.Int(bm.Get("astaxie"), err); v != 2 {
52 t.Error("get err") 52 t.Error("get err")
53 } 53 }
54 54
...@@ -74,8 +74,8 @@ func TestRedisCache(t *testing.T) { ...@@ -74,8 +74,8 @@ func TestRedisCache(t *testing.T) {
74 if v, _ := redis.String(bm.Get("astaxie"), err); v != "author" { 74 if v, _ := redis.String(bm.Get("astaxie"), err); v != "author" {
75 t.Error("get err") 75 t.Error("get err")
76 } 76 }
77 // test clear all 77 // test clear all
78 if err = bm.ClearAll(); err != nil { 78 if err = bm.ClearAll(); err != nil {
79 t.Error("clear all err") 79 t.Error("clear all err")
80 } 80 }
81 } 81 }
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!