bf836a21 by astaxie

fix #233

thanks for the good suggestion
1 parent 35a136bc
...@@ -36,6 +36,9 @@ func NewCache(adapterName, config string) (Cache, error) { ...@@ -36,6 +36,9 @@ func NewCache(adapterName, config string) (Cache, error) {
36 if !ok { 36 if !ok {
37 return nil, fmt.Errorf("cache: unknown adaptername %q (forgotten import?)", adapterName) 37 return nil, fmt.Errorf("cache: unknown adaptername %q (forgotten import?)", adapterName)
38 } 38 }
39 adapter.StartAndGC(config) 39 err := adapter.StartAndGC(config)
40 if err != nil {
41 return nil, err
42 }
40 return adapter, nil 43 return adapter, nil
41 } 44 }
......
...@@ -22,7 +22,11 @@ func NewRedisCache() *RedisCache { ...@@ -22,7 +22,11 @@ func NewRedisCache() *RedisCache {
22 22
23 func (rc *RedisCache) Get(key string) interface{} { 23 func (rc *RedisCache) Get(key string) interface{} {
24 if rc.c == nil { 24 if rc.c == nil {
25 rc.c = rc.connectInit() 25 var err error
26 rc.c, err = rc.connectInit()
27 if err != nil {
28 return nil
29 }
26 } 30 }
27 v, err := rc.c.Do("HGET", rc.key, key) 31 v, err := rc.c.Do("HGET", rc.key, key)
28 if err != nil { 32 if err != nil {
...@@ -33,7 +37,11 @@ func (rc *RedisCache) Get(key string) interface{} { ...@@ -33,7 +37,11 @@ func (rc *RedisCache) Get(key string) interface{} {
33 37
34 func (rc *RedisCache) Put(key string, val interface{}, timeout int64) error { 38 func (rc *RedisCache) Put(key string, val interface{}, timeout int64) error {
35 if rc.c == nil { 39 if rc.c == nil {
36 rc.c = rc.connectInit() 40 var err error
41 rc.c, err = rc.connectInit()
42 if err != nil {
43 return err
44 }
37 } 45 }
38 _, err := rc.c.Do("HSET", rc.key, key, val) 46 _, err := rc.c.Do("HSET", rc.key, key, val)
39 return err 47 return err
...@@ -41,7 +49,11 @@ func (rc *RedisCache) Put(key string, val interface{}, timeout int64) error { ...@@ -41,7 +49,11 @@ func (rc *RedisCache) Put(key string, val interface{}, timeout int64) error {
41 49
42 func (rc *RedisCache) Delete(key string) error { 50 func (rc *RedisCache) Delete(key string) error {
43 if rc.c == nil { 51 if rc.c == nil {
44 rc.c = rc.connectInit() 52 var err error
53 rc.c, err = rc.connectInit()
54 if err != nil {
55 return err
56 }
45 } 57 }
46 _, err := rc.c.Do("HDEL", rc.key, key) 58 _, err := rc.c.Do("HDEL", rc.key, key)
47 return err 59 return err
...@@ -49,7 +61,11 @@ func (rc *RedisCache) Delete(key string) error { ...@@ -49,7 +61,11 @@ func (rc *RedisCache) Delete(key string) error {
49 61
50 func (rc *RedisCache) IsExist(key string) bool { 62 func (rc *RedisCache) IsExist(key string) bool {
51 if rc.c == nil { 63 if rc.c == nil {
52 rc.c = rc.connectInit() 64 var err error
65 rc.c, err = rc.connectInit()
66 if err != nil {
67 return false
68 }
53 } 69 }
54 v, err := redis.Bool(rc.c.Do("HEXISTS", rc.key, key)) 70 v, err := redis.Bool(rc.c.Do("HEXISTS", rc.key, key))
55 if err != nil { 71 if err != nil {
...@@ -60,7 +76,11 @@ func (rc *RedisCache) IsExist(key string) bool { ...@@ -60,7 +76,11 @@ func (rc *RedisCache) IsExist(key string) bool {
60 76
61 func (rc *RedisCache) Incr(key string) error { 77 func (rc *RedisCache) Incr(key string) error {
62 if rc.c == nil { 78 if rc.c == nil {
63 rc.c = rc.connectInit() 79 var err error
80 rc.c, err = rc.connectInit()
81 if err != nil {
82 return err
83 }
64 } 84 }
65 _, err := redis.Bool(rc.c.Do("HINCRBY", rc.key, key, 1)) 85 _, err := redis.Bool(rc.c.Do("HINCRBY", rc.key, key, 1))
66 if err != nil { 86 if err != nil {
...@@ -71,7 +91,11 @@ func (rc *RedisCache) Incr(key string) error { ...@@ -71,7 +91,11 @@ func (rc *RedisCache) Incr(key string) error {
71 91
72 func (rc *RedisCache) Decr(key string) error { 92 func (rc *RedisCache) Decr(key string) error {
73 if rc.c == nil { 93 if rc.c == nil {
74 rc.c = rc.connectInit() 94 var err error
95 rc.c, err = rc.connectInit()
96 if err != nil {
97 return err
98 }
75 } 99 }
76 _, err := redis.Bool(rc.c.Do("HINCRBY", rc.key, key, -1)) 100 _, err := redis.Bool(rc.c.Do("HINCRBY", rc.key, key, -1))
77 if err != nil { 101 if err != nil {
...@@ -82,7 +106,11 @@ func (rc *RedisCache) Decr(key string) error { ...@@ -82,7 +106,11 @@ func (rc *RedisCache) Decr(key string) error {
82 106
83 func (rc *RedisCache) ClearAll() error { 107 func (rc *RedisCache) ClearAll() error {
84 if rc.c == nil { 108 if rc.c == nil {
85 rc.c = rc.connectInit() 109 var err error
110 rc.c, err = rc.connectInit()
111 if err != nil {
112 return err
113 }
86 } 114 }
87 _, err := rc.c.Do("DEL", rc.key) 115 _, err := rc.c.Do("DEL", rc.key)
88 return err 116 return err
...@@ -99,19 +127,23 @@ func (rc *RedisCache) StartAndGC(config string) error { ...@@ -99,19 +127,23 @@ func (rc *RedisCache) StartAndGC(config string) error {
99 } 127 }
100 rc.key = cf["key"] 128 rc.key = cf["key"]
101 rc.conninfo = cf["conn"] 129 rc.conninfo = cf["conn"]
102 rc.c = rc.connectInit() 130 var err error
131 rc.c, err = rc.connectInit()
132 if err != nil {
133 return err
134 }
103 if rc.c == nil { 135 if rc.c == nil {
104 return errors.New("dial tcp conn error") 136 return errors.New("dial tcp conn error")
105 } 137 }
106 return nil 138 return nil
107 } 139 }
108 140
109 func (rc *RedisCache) connectInit() redis.Conn { 141 func (rc *RedisCache) connectInit() (redis.Conn, error) {
110 c, err := redis.Dial("tcp", rc.conninfo) 142 c, err := redis.Dial("tcp", rc.conninfo)
111 if err != nil { 143 if err != nil {
112 return nil 144 return nil, err
113 } 145 }
114 return c 146 return c, nil
115 } 147 }
116 148
117 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!